In .htaccess i want to rewrite the URL so...
http://example.com/folder/file.php?folder/images/image0001.jpg
Becomes...
http://example.com/folder/images/image0001.jpg
I have been trying...
RewriteRule ^folder/file.php?(.*)$ http://example.com/$1 [R=301,L]
But ends up with a unwanted ?-sign...
http://example.com/?folder/images/image0001.jpg
How do i get rid og the ?-sign ?
You can't match against the ? in a rewrite rule, you need to match against either in a %{QUERY_STRING} or in %{THE_REQUEST}. If you want to rewrite it to http://example.com/folder/images/image0001.jpg, meaning that the file **is actually at /folder/images/image0001.jpg, then you want this:
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^folder/file\.php$ http://example.com/%1? [R=301,L]
If there isn't anything there, obviously that redirect will just result in a 404 Not Found. If this is supposed to be a way to make the URL look "pretty", then you need to rewrite the URL back to the query string:
RewriteCond %{THE_REQUEST} \ /+folder/file\.php\?([^&\ ]+)
RewriteRule ^folder/file\.php$ http://example.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*)$ /folder/file.php?folder/$1 [L]
Related
RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]
I'm trying to allow my site to rewrite urls. I have put the following into my .htaccess file in the root directory.
RewriteEngine On
#would be nice to remove member-pages from the URL but no idea how.
#RewriteRule ^members/(.*)/?$ /$1 [NC,R]
#This part works though!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ ./members/$1/ [L]
So far, it takes
mydomain.com/someUserName or mydomain.com/someUserName/ (with trailing slash) and, if it exists, will load the page at mydomain.com/members/someUserName/ without a hitch. This works like a gem.
What I want now (and am trying to do with the first rewrite rule) is to take a mydomain.com/members/someUserName or mydomain.com/members/someUserName/ and have it show up as mydomain.com/someUserName in the url.
How do I do this? Thanks in advance!
If I understand you correctly, You want to redirect domain.com/members/foo to domain.com/foo , You can use the following rule for that:
RewriteEngine On
RewriteCond %{THE_REQUEST} /memebers/([^\s]+) [NC]
RewriteRule ^ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ ./members/$1 [NC,L]
in the same folder where there is index.php I have .htaccess file with the following code inside:
RewriteEngine On
RewriteRule ^example.com/(.+)$ example.com/secondpage.php?firmname=$1 [R,L]
so that instead of
example.com/secondpage.php?firmname=GGGGGGG
it should show
example.com/GGGGGGG
but it doesn't work and I cant find mistake there...
You don't want the hostname in the regex and rule's target. It's also showing the URL with the query string because you are using the R flag, which redirects the browser and thus changes the address in the location bar.
You probably want something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /secondpage.php?firmname=$1 [L]
And maybe:
RewriteCond %{THE_REQUEST} \ /+secondpage\.php\?firmname=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
I have the following htaccess rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
What this does is insert a trailing slash character to all my URLs, i.e:
http://localhost/advertising becomes http://localhost/advertising/
Now I have some URLs such as: http://localhost/contact.html
But my htaccess rule is applying the slash to these URLs too - so the above URL becomes http://localhost/contact.html/
Now I added the following condition to prevent this happening:
RewriteCond %{REQUEST_URI} !\.(html|php)$
But I want to be a bit more clever than this - I want a single expression that will match ANY extension the user types in. How can I achieve this?
This will rewrite all directories without trailing slash!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)(\..{3,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
I don't have much experience with htaccess and mod_rewrite or anything, but how would one remove the ? from a URL that looks something like: site.com/?page so it looks like site.com/page ?
I'm trying this at the moment:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
But it just redirects everything to just site.com index page, without anything else in the url.
Your backreference is incorrect. You want to match the 2nd grouping from %{THE_REQUEST}, not the first (which is index.php). So your first rule should look like:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%2? [L,R=301]
# 2 here--------^
The 2nd grouping would be the ([^&\ ]+) match.