So right now I'm just trying to convert something like
https://gregorj.org/post?id=26
to
https://gregorj.org/post/26
Everything I've found (also on stackoverflow) is telling me to put this in .htaccess:
RewriteEngine On
RewriteRule ^post/([^/.]+)?$ /post?id=$1 [L]
But it's not working in my case. I'm assuming this might just be because I have a bunch of other stuff in my .htaccess, that I put there to hide .php and .html extensions back when I started the site (which is also why I wrote /post?id=$1 and not /post.php?id=$1 in .htaccess - but I've tried both just in case :).
Here's my .htaccess as it stands today:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R,L]
# Prevent POST requests from getting redirected
RewriteCond %{REQUEST_METHOD} !POST
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Any ideas as to what I can do to get the prettier links?
Well, I answered my own question.
If I start out preventing POST requests from getting redirected, then maybe ^post isn't a great way to start a redirect rule...
I changed it to ^article and no problems
Related
I need help here.
I want apply a slash at the end of my urls with htaccess, but unfortunately its not working this way, maybe you guys can help me here?
This is my htaccess:
RewriteEngine on
RewriteBase /
## hide .php extension except in POST method
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I tried add this rule, but do not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
I appreciate any help here.
Thanks :)
My htaccess file is interfering with the implementation of Dropzone.js on my website. Here is the htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
#Force non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Options -Indexes
Basically I am forcing a non-www redirect and am hiding the .php extension from the URL. However, I did not know that that would interfere with Dropzone.js, which requires a file name in the action attribute of the form or in the url property of the Dropzone object.
How can I hide the .php extension without interfering with Dropzone.js?
Thanks to everyone! I took out the .php extension from the action attribute and like Enyo suggested that fixed it.
Hi I am updating a invoice system and we won't to redirect any http request using /client-area/viewinvoice.php/?id=xx to /client-area/viewinvoice/?id=xx basically removing the php extension. We can't remove them entirely as some pages still use them.
How do you do this via htaccess?
Thanks for the help
put this code in your DOCUMENT_ROOT/.htaccess file for .php removal:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I want to get all my domains to rewrite and look good. for example:
example.com/blog.php?article=the-name-of-article
to become
example.com/blog/the-name-of-article
so i want the .php?article= to be changed into a /
my current .htaccess file looks like:
Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /
## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
but all that does is drop the .php
I have been searching around but I just want to hear how everyone accomplishes this so easily.
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]
You had a great response that helped me resolve my .htaccess issue on GoDaddy. This was the code you provided:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
QUESTION: How can I exclude a file and/or directory from being rewritten?
Thanks!
David
Assuming you don't want to redirect /ignore.php. Modify above code like this:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_URI} !^/ignore\.php$ [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !^/ignore$ [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]