I copied the below code from some other posts. It successfully helped me to stripped the .php extension from the URL. However, after implementing this, my forms are no longer working. I have forms that are posting like <form method='post' action='login.php' .... Now it is not sending the data to the location when I submit the form. I realize that if I change the action='login.php' to action='login', the forms will work again. But there are a number of pages that contain forms posting to different locations in my website, and I do not want to change so many of them manually. So I wouold like to know why the data are not posting as expected and how can I solve the problem. Thank you!
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/domainname/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_FILENAME} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/domainname/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/domainname/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Add this rule on top before other rules to skip all POST requests from rewrite:
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]
I want to redirect user_login.php to seo friendly url like /user-login with post form data and this worked for me.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/user_login\.php [NC]
RewriteRule ^ user-login [QSA,R=301]
RewriteRule ^user-login$ user_login.php [QSA,L]
In view file
<form action="<?php $siteurl;?>/user-login" method="post" id="user_login">
Related
I redirect the news with the folling code in htaccess:
RewriteCond %{REQUEST_URI} news-detail\.html
RewriteCond %{QUERY_STRING} L=0 [NC]
RewriteCond %{QUERY_STRING} (tx_news_pi1\[news\]=101|tx_news_pi1%5Bnews%5D=101) [NC]
RewriteRule ^.*$ http://example.net/news/news-detail/8/2016/newstitle? [R=301,L]
The redirect works. Is it possible to use the parameter from QUERY_STRING in RewriteRule? So that every news article is automatically redirected to speaking url. I tried to use some of the parameters from config file but it doesn't work.
You don't get the news title in htacces by the id uid only.
I propose to leave url like they are, use realurl and set a canonical url to the realurl variant
I have created a www.example.com and m.example.com. The m.example.com resides inside the htdocs/m/ folder. I am also deleting the '.php' extensions using .htaccess. I am having the below codes on the root directory:
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#mobile
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteRule ^(.*)$ m/$1 [L]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I have forms in the m.example.com. For example in m.example.com/myform.php, I have
<form method="post" action="processform.php">
.....
</form>
I want it to be submitted to the m/ folder (/m/processform.php), but now it always submit to the processform.php of the main directory. I have tried action="//m.example.com/processform.php" but no luck.
Anyone know kow to solve it? Thanks!
Update:
if the method of the form is get instead of post, the form will be submitted correctly (to /m/processform.php)
Actually your rules are skipping POST requests from all rewrite rules. You can tweak the rules to skip POST from external redirects and keep internal rewrites active/enabled for POST like this:
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#mobile
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteRule ^((?!m/).*)$ m/$1 [L,NC]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
PS: I have also simplified your 2nd rule.
It's possible the first redirect is interferring with the POST request, try changing the request regex to:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
I need to rewrite all possible request to the path of the server
Example: If I enter https://wwww.myhost.com/#hi=random+text+1?stop=now
I need a redirect to:
http://myhost.com/
It is possible with url.rewrite?
Thanks
What are you really trying to achieve? Do you want to hide the urls so that it always shows http://myhost.com. I believe that is not possible if you have more than one page.
Are you trying to redirect everything to for example index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?anything=$1 [QSA,L]
This way for example http://myhost.com/example/ will be handled like the request was http://myhost.com/index.php?anything=example
rewriting the url RewriteRule ^about/ /about.php [L,R=301] but the page is accessed via /about.php directly also. Please help to how to redirect .php file
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+about\.php\s [NC]
RewriteRule ^ /about/ [R=302,L]
RewriteRule ^about/?$ /about.php [L,NC]
You can use this free .htaccess Redirect Generator:
http://htaccessredirect.net/
with this tool you can easily create your redirection code for .htaccess.
You can use rule to hidden redirect:
#use [L] to hidden [R] to shown redirect
RewriteRule ^(about|faq|home)/?$ $1.php [L]
#to prevent access from about.php simply do this:
RewriteRule (.+)\.php$ $1/ [R=301]
#you need to know 301 redirect will be cached with browser for long time
#so if you change rule - will be no effect for some visitors
This is my mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
# ————————————————————————-
# > URL REWRITING
# ————————————————————————-
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)/diario$ hotsite/diary.php
RewriteRule ^(.*)/recados$ hotsite/messages.php
RewriteRule ^(.*)/fotos$ hotsite/photos.php
RewriteRule ^(.*)/videos$ hotsite/videos.php
RewriteRule ^(.*)/contato$ hotsite/contact.php
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
It works just like facebook profiles. Whenever I type "mywebsite.com/user.name" it goes to that user page. I can also type "mywebsite.com/user.name/videos" to go to a specific page in the user profile.
However, I can't access "mywebsite.com" anymore, because it redirects to "mywebsite.com/hotsite/index.php". How can I disable this behavior and leave the redirects only when someone types a user name at the end?
Thank you very much.
The RewriteCondition(s) only affect the RewriteRule that comes immediately after that. So, to stop redirecting htp://mywebsite.com/aDirThatExists like http://mywebsite.com/aboutUs you need to repeat the rewrite conditions as
RewriteCond %{SCRIPT_FILENAME} !-d # if not a directory
RewriteCond %{SCRIPT_FILENAME} !-f # and not a file
RewriteRule ^([a-z0-9._\-]+)$ hotsite/index.php [L]
This, however, should not affect a root URL request i.e. http://mywebsite.com/ because your regex clearly matches one or more characters after / because of the []+ plus sign.