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 many possible solutions, and none of them worked.
Url structure will be /jobs/job-***.html?something and which I need to redirect to /jobs/job-***.html
I tried something like this, but returned 500 error;
RewriteRule ^jobs/job-([0-9]+).html?$ jobs/job-$1 [NC, L]
Please help to solve this problem.
You may use this rule:
RewriteCond %{QUERY_STRING} .
RewriteRule ^jobs/job-(\d+\.html?)$ %{REQUEST_URI}? [NC,L,R=301]
? after $1 in target will remove any query string.
You may use the following regex:
^\/jobs\/job-(\d+)\.html\?\S*$
(This essentially captures the job number)
and then replace it with:
/jobs-$1.html
Demo
Related
I want to replace this URL:
mydomain.com/posts/1659-artigos/etc-to
By this one:
mydomain.com/etc-to
Using .htaccess I'm trying the following:
RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ $1
But it isn't working. No redirect happens.
Can anyone help?
Thanks!
Converting my comment to answer so that solution is easy to find for future visitors.
You can use this code to get redirect working:
RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ /$1 [L,R=301,NC]
You need to use / before $1 for external redirect and make sure to use R flag for full redirect.
I'm new to the rewriting of urls and regex in general. I'm trying to rewrite a URL to make it a 'pretty url'
The original URL was
/localhost/house/category.php?cat=lounge&page=1
I want the new url to look like this:
/localhost/house/category?lounge&page=1
(like I say, I'm new so not trying to take it too far at the moment)
the closest I've managed to get it to is this:
RewriteRule ^category/(.*)$ ./category.php?cat=$1 [NC,L]
but that copies the whole URL and creates:
/localhost/house/category/house/category/lounge&page=1
I'm sure, there must be an easy way to say copy all after that expression, but I haven't managed to get there yet.
I will try to help you:
You probably have already, but try a mod rewrite generator and htaccess tester.
From this answer: The query (everything after the ?) is not part of the URL path and cannot be passed through or processed by RewriteRule directive without using [QSA].
I propose using RewriteCond and using %1 instead of $1 for query string matches as opposed to doing it all in RewriteRule.
For your solution, try:
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^house/category$ house/category.php?cat=%1 [NC,L]
This will insert the .php and cat= while retaining the &page=
Anticipating your next step, the below mod rewrite may help get started in converting
http://localhost/house/category/lounge/1
to
http://localhost/house/category.php?cat=lounge&page=1
Only RewriteRule necessary here, no query string:
RewriteRule ^house/category/([^/]*)/([0-9]*)/?$ house/category.php?cat=$1&page=$2 [NC,L]
Use regex101 for more help and detailed description on what these regexes do.
If it still not working, continue to make the regex more lenient until it matches correctly:
Try to remove the ^ in RewriteRule so it becomes
RewriteRule category$ category.php?cat=%1 [NC,L]
Then it will match that page at any directory level. Then add back in house/ and add /? wherever an optional leading/trailing slash may cause a problem, etc.
Thanks for all your suggestions, I took it back to this
RewriteRule category/([^/])/([0-9])/?$ category.php?cat=$1&page=$2 [NC,L]
which has done the trick, and I'll leave it at this for now.
I am using Apache 2.4.7. I use mod_rewrite to alter some urls.
I want to rewrite http://example.com/servicename/oldpage?id=abcto http://example.com/servicename/newpage.
Other similar rewrites work so I belive the ? inside url is causing problems.
I have tried escaping it with \.
This works as there is no ? in url:
RewriteRule ^/servicename/old /servicename/new
But these don't work:
RewriteRule ^/servicename/oldpage?id=abc /servicename/newpage
RewriteRule ^/servicename/oldpage\?id=abc /servicename/newpage
I have also tried using RewriteCond from examples like this: .htaccess rewrite URL with a question mark "?" but I didn't manage to get them work.
How should rewrite url that contains question mark?
EDIT: I tried solutions given in Match Question Mark in mod_rewrite rule regex but was not able to make them work for me. That question is about preserving query string when rewrite while I want to remove it when rewriting.
RewriteRule pattern is matched against the part of the URL after the hostname and port, and before the query string.
When the requested URI contains a query string, and the target URI does not, the default behavior of RewriteRule is to copy that query string to the target URI. Using the [QSD] flag causes the query string to be discarded.
So, this should work:
RewriteRule ^/servicename/oldpage /servicename/newpage [QSD]
I would like to ask you guys because I have a problem with one my rewrite rules and I can't figure it out how to write the good one.
I have this rule:
RewriteRule ^wp-content/uploads/(.*[^(.js|.swf)])$ authenticate.php?file=$1
What I would like to do is redirect the user to the authenticate.php every time when someone tries to open something in the wp-content uploads dir and I would like to send the filename to the php
For example:
http://domain.tld/wp-content/uploads/2015/11/something.pdf
redirect to authenticate.php?file=something.pdf
But unfortunately my regexp is broken. Could someone help me?
Really thanks for it!
Try with that in your .htaccess:
RewriteCond %{REQUEST_URI} !(?:js|swf)$ [NC]
RewriteRule ^wp-content/uploads/(.+)$ authenticate.php?file=$1 [NC,L]
For http://domain.tld/wp-content/uploads/2015/11/something.pdf the result: http://domain.tld/authenticate.php?file=2015/11/something.pdf
Try using this regex with negative lookaheads:
^.*?wp-content\/uploads\/.*?\.(?!js$|swf$)[^.]+$
The following URL will match the regex:
http://cpsma.ie/wp-content/uploads/2015/09/CPSMA-Newsletter-No-35-Sept-2015-2.pdf
However, a similar URL which ends in .js or .swf will not match. Hence, the following two URLs do not match the regex:
http://domain.tld/wp-content/uploads/2015/10/javascript.js
http://domain.tld/wp-content/uploads/2015/02/shockwavefile.swf
You can test this regex out here:
Regex101
I can rewrite mydomain.comto www.mydomain.com, that's OK.
But, I just couldn't figure how to rewrite:
http://mydomain.com/great-article to http://www.mydomain.com/great-article.
How can this be done with regex? Any help would be appreciated.
This shall work:
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule .* http://www.mydomain.com/$0 [L,R=301,QSA]
I.e. every request to
http://mydomain.com/something
will get redirected to
http://www.mydomain.com/something.
As well as simple requests
http://mydomain.com/
will go to
http://www.mydomain.com/.
And due to Query string append (QSA) - this shall work for such URI
http://mydomain.com/index.php?action=hello¶m=world
to get redirected to
http://www.mydomain.com/index.php?action=hello¶m=world.
Or am I missing something?