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]
Related
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
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.
I got a .htaccess that pretty much looks like this:
# disable directory browsing
Options All -Indexes
# start rewrites
RewriteEngine on
Redirect 301 /someoldpage.php http://example.com/fancyurl
# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
What I want to accomplish is that the url is rewritten also when someone is trying to access an old 301 redirectred url, so when going to http://example.com//someoldpage.php you see the url http://example.com/fancyurl and NOT like now: http://example.com/index.php?page=fancyurl
Redirect is part of mod_alias, while the rewrite stuff is part of mod_rewrite. The two different modules both get applied to the same request and thus causing it to redirect and rewrite at the same time. What you want to do is have it redirect if the request is /someoldpage.pjp and not rewrite to index.php, so you need to use only mod_rewrite here:
# disable directory browsing
Options All -Indexes
# start rewrites
RewriteEngine on
RewriteRule ^someoldpage\.php$ http://example.com/fancyurl [L,R=301]
# if not a file, and not a directory, reroute through index as normal page
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
I Struck with this problem and got a solution in my case, to rewrite the url from example.com/about.php to example.com/about with the following code in your htaccess.
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_FILENAME}.php [L]
I have an htaccess file under the main site
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php or .html extension
# To externally redirect foo.php ot foo.html to foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.(php|html) [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
That works for
http://www.site.com/tenant-referencing/
but I can't get the subsite to work
http://www.site.com/newwebsite/tenant-referencing/
I have the same htaccess under the subsite
Thanks
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]