I'm trying to rewrite url from
example.com/test/?var=somevalue or example.com/test/page1.html?var=somevalue
to
example.com/test/somevalue/
Here is the code
RewriteCond %{REQUEST_URI} test/$
RewriteCond %{QUERY_STRING} var=([a-z]*|[a-z]*\-[a-z]*)$
RewriteRule ^test/(.*)$ /test/%1/page1.html? [R=301,L]
This for now only works with url example.com/test/?var=somevalue and what I can't get to work is url with page[1-9][0-9]*\.html before query_string
RewriteCond %{REQUEST_URI} test/(page[1-9][0-9]*\.html)?$
I think that's what you're looking for, piecing together the regex you already used with the one you described at the bottom of your query. But I'm a little shocked your rewrite rule worked at all. I would expect something more along the lines of:
RewriteRule ^test/([^/?])*\?([^&]+&)*var=([^&]+) /test/%3/page1.html
But I don't write that many RewriteRules, so I could be wrong.
So something like this?
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /test/([^\?]*)\?var=([^&\ ]+)([^\ ]*)
RewriteRule ^ /test/%3/%2?%4 [L,R=301]
Related
I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.
I've some dynamic urls. If I took a url with a query parameter, it leads to 404 page. So I would like to do a redirection using htaccess. I tried with a matching url pattern regex and it is not redirecting.
Url structure will be /detail/2019-12/news/news-title-12-2019.html?something and which I need to redirect to /detail/2019-12/news/news-title-12-2019.html
I tried something like this, but it is not redirecting;
RewriteCond %{QUERY_STRING} .
RewriteRule ^detail/\d{4}-\d{2}/news/(?=\S*['-])([a-zA-Z0-9'-]+\.html)\?\S*$ %{REQUEST_URI}? [NC,L,R=301]
How do I solve this problem?
This RegEx might help you to write your RewriteRul:
^(.+\.html)(\?.+)$
I'm not quite sure, but your code might look like:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=301]
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=302]
You also might need to:
restart apache
delete your browser cache
If you wish to add boundaries to your expression, this RegEx might help you to do so:
^(\/detail\/[0-9-]+\/news\/[a-z0-9-]+\.html)(\?.+)$
Code:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(\/detail\/[0-9-]+\/news\/[a-z0-9-]+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=301]
RewriteCond %{QUERY_STRING} .
RewriteRule ^(\/detail\/[0-9-]+\/news\/[a-z0-9-]+\.html)(\?.+)$ http://domain_goes_here/$1 [NC,L,R=302]
The reason why you rule isn't working is because you are checking Querystring ? in your rule regex. The pattern of RewriteRule is for URL path only .
The following should work for you
RewriteCond %{QUERY_STRING} .
RewriteRule ^detail/\d{4}-\d{2}/news/ %{RRQUEST_URI}? [L,R=301]
Make sure to clear your browser cache or use a different browser for testing this.
Our original urls began with a parameter and the following rewrite works to redirect them (thanks to Jon Lin).
However, the original parameter is being appended to redirect, so that
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$
RewriteRule ^$ /newpage [R=301,L]
ends up going to mydomain.com/newpage?old-page
Any idea how to fix? thanks again,
Geoff
Have it like this to strip off existing query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$ [NC]
RewriteRule ^$ /newpage? [R=301,L]
Take note of ? at the end of target URI, that is used to strip-off existing query string.
I tried the below .htaccess rewrite to internally rewrite the urls and make the overall urls SEF.
RewriteCond %{QUERY_STRING} ^searchword=(.*)&(.*)$ [NC]
RewriteRule ^searchword=(.*)&(.*)$ searchword=$1 [L]
My url is of the following form.
http://www.domain.com/?searchword=search+term&Search=&searchphrase=all&limit=50&ordering=newest&view=search&option=com_search
I want to make it of the form
http://www.domain.com/?searchword=search+term
You can use this rule:
RewriteCond %{QUERY_STRING} ^(searchword=[^&]+)$ [NC]
RewriteRule ^ /?%1&Search=&searchphrase=all&limit=50&ordering=newest&view=search&option=com_search [L,NE]
The best way to do this is by adding a router.php file to your component. Take a look at http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
I am trying to configure a dynamic mod rewrite rule in my .htaccess file using mod_rewrite. I am very close to figuring this out. I am trying to get URLs like these:
http://www.mysite.com/index.php?service=14&title=events
http://www.mysite.com/index.php?service=48&title=planning
To automatically be rewritten to these:
http://www.mysite.com/service/14/events
http://www.mysite.com/service/48/planning
Here is my codes so far:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} ^service=$1&title=$2 [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php [NC]
RewriteRule ^index.php$ /service/$1/$2 [L,R=301]
I think there is something wrong with the last line maybe? I'm not the best at regular expressions so any help would be greatly appreciated.
Edit: Just wanted to be clear that the pretty URLs do work. However, the old URLs aren't redirecting and are still displaying in the browser.
I am not sure if this would be any easier but if I could get the URLs to look like this:
http://www.mysite.com/service/14/title/events
http://www.mysite.com/service/48/title/planning
Then that would work too. I don't really need the second query title to be in the URL but if it's easier to leave it in there, then no big deal.
Edit: Answered
Many thanks to all who helped contribute to the solution. I got this for my rewrite rule:
RewriteRule ^index/service/(.*)/(.*)/$ index.php?service=$1&title=$2
Once I added 'index' it worked with both query strings. As far as the redirect goes, I edited my php script and did all the URL redirecting there, which was a lot easier. Special thanks to mkjasinski for pointing that out.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^service/([0-9]+?)?/([a-zA-Z\-]+?)$ index.php?service=$1&title=$2 [L,NC]
and if I have run: http://localhost/service/12/This-is-text in $_GET in index.php:
array (size=2)
'service' => string '12' (length=2)
'title' => string 'This-is-text' (length=12)
you can't use special holders ($1,$2...) from previous rewrite rules into rewritecond.
change your code to:
RewriteRule ^service/(.*)/(.*)$ index.php?service=$1&title=$2 [NC,L]
RewriteCond %{QUERY_STRING} (?:^|&)service=([0-9]+)(&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)title=([a-z]+)(&|$) [NC]
RewriteRule ^index.php$ /service/%1/%3? [L,R=301]