I'm really bad at htaccess, I can't figure this out.
I have the following file:
/site/.htaccess
I would like to create a rewrite that would rewrite:
/site/en/home/
to:
/site/home/?lang=en
and (var is just an example, it should work with any query string):
/site/en/home/?var=12345
to:
/site/home/?var=12345&lang=en
How can I accomplish this? I've been at it for 1 hour and I can't get it to work.
You can use this in /site/.htaccess
RewriteEngine on
RewriteRule ^en/(.+)/?$ /site/$1/?lang=en [QSA,L]
QSA QueryStringAppand flag automatically appends the addtional queryString to the target.
Related
I need to redirect an incoming request with the following URL:
http://mywebsite.com/abc/mapserv.exe?map=123
to
http://mywebsite.com/abc/mapserv.exe?map=C:\Mapserver\ms4w\Apache\htdocs\Mapfiles\123.map
I already managed to do simple mod_rewrites but the question mark is killing this one all the time. I am not able to adapt common Query String examples to my case so I need help with this exact case.
As though you did not show your try, you could test this:
RewriteEngine On
RewriteCond %{QUERY_STRING} map=([0-9]+)$
RewriteRule . %{REQUEST_URI}?map=C:\\Mapserver\\ms4w\\Apache\\htdocs\\Mapfiles\\%1.map [NE,L]
Rewrite flags used:
NE: Not Escape,
L: Last instruction to run.
I was still having trouble with the .exe url since it is not accessible if you dont deliver the parameters right when you send the request. And then the redirect wont fire. So I made a dummy mapserver.php file which allows setting a parameter like so:
http://mywebsite.com/abc/mapserver.php?map=123
After hours of trying I ended up with the following RewriteRule:
RewriteCond %{QUERY_STRING} ^map=(.*)$
RewriteRule ^mapserver.php?$ /cgi-bin/mapserv.exe?map=C://Mapserver//ms4w//Apache//htdocs//Mapfiles//%1.map
How can I rewrite url from this:
https://www.domain.com/blog/category/sub-category-name
to this:
https://www.domain.com/blog/category/?category=sub-category-name
also it must work with others parameters like:
https://www.domain.com/blog/category/sub-category-name?param=value => https://www.domain.com/blog/category/?category=sub-category-name¶m=value
or
https://www.domain.com/blog/category/sub-category-name#hash => https://www.domain.com/blog/category/?category=sub-category-name#hash
I don't understand this rewrite syntax so any advice is valuable.
Thanks
You can use this rule :
RewriteEngine on
RewriteRule ^blog/([^/]+)/([^/]+)/?$ /blog/$1/?$1=$2 [QSA,NC,L]
Additional Query perameters are automatically appended to the target path by QSA flag.
I need to append all url's from a folder with a query param using Apache rewrite rule.
For this all url's starting with /abc/def/xyz/ the url should be appended with ?v=2
For example, /abc/def/xyz/folder/test.pdf should become /abc/def/xyz/folder/test.pdf?v=2
I tried with RewriteRule /abc/def/xyz(.*) /abc/def/xyz/$1?v=2 but it is not working.
I think you've got it only slightly wrong (start of the pattern), try this:
RewriteRule ^abc/def/xyz/(.*) /abc/def/xyz/$1?v=2
Tested here:
http://martinmelin.se/rewrite-rule-tester/
How can i catch the prams after file name with extension like service.php/view1
for ex:
service.php/newview1
I want to get it like
service.php?view=newview1
how do i write mod-rewrite for this
I tried like
RewriteRule ^services.php/?([a-zA-Z_]+)$ /services.php?category=$1
its not matching the service.php/newview1
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the query
strings to be combined.
You need to add [QSA] flag to end of your current line
Try adding this regex:
([a-zA-Z0-9-_]+) as this also matches any integers and hyphens also.
RewriteRule ^services.php/?([a-zA-Z0-9-_]+)$ /services.php?category=$1 [QSA,L]
I also put the [L] flag so it stop processing further rules.
I'm having an issue with conflicting quert strings on a page. for instance the page can look like the following:
website.com/photos/type/album-name/page-2/
AND
website.com/photos/type/album-name/photo-id/
the issue here is that I don't know how to make it so that it knows that when it says 'page-2' that it takes one rule, and when it's the photo-id it takes another.
I have the following
RewriteRule ^photos/([\w+.-]+)/([\w+.-]+)/([\w+.-]+)/?$ /photos.php?view=$1&slug=$2&page=$3 [QSA,L]
RewriteRule ^photos/([\w+.-]+)/([\w+.-]+)/([0-9]+)/?$ /photos.php?view=$1&slug=$2&pid=$3 [QSA,L]
Instead of having different rewrite rules for every possible query string, why don't you have a generalized rewrite and handle the query strings in your application?
Or, for your second query, try this:
RewriteRule ^photos/([\w+.-]+)/([\w+.-]+)/(page\-[0-9]+)/?$ /photos.php?view=$1&slug=$2&pid=$3 [QSA,L]
Your URLs would look like,
website.com/photos/type/album-name/page-2/ --> with the 'page-' keyword.