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
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'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
RewriteRule ^categories/([A-Za-z0-9\-]+)/?$ /categories.php?c=$1 [QSA,L]
This is my RewriteRule, it deals with categories such as /categories/Family perfectly that URL displays the page as I would like it too, However with something such as /categories/Web%20Design I get an The requested URL /categories/Web Design was not found on this server.
This is a pain, I've even tried to use a space in the ReWriteRule after the 9 in [A-Za-z0-9-], what's the best way to handle spaces in URLs with the rewrites?
Thank You All.
Use this rule by including space in your character class:
RewriteRule ^categories/([A-Za-z0-9\s-]+)/?$ /categories.php?c=$1 [QSA,L]
try adding \s to the Regex...
That matches whitespace.
([A-Za-z0-9\-\s]+)
I searched in the web to find a valid solution for my problem, but nothing works.
Hopefully you guys can help me.
I want to Rewrite a URL on an Apache doing like that:
(1.) www.example.com/en/rainbow.html => www.example.com/index.php?site=rainbow&lang=en
or
(2.) www.example.com/rainbow.html =>www.example.com/index.php?site=rainbow&lang=
or
(3.) www.example.com//rainbow.html =>www.example.com/index.php?site=rainbow&lang=
To be honest, my understanding for regex isn't that good.
Tried:
RewriteEngine On
RewriteRule ^(.+)[/](.+).html|(.+).html$ index.php/?lang=$1&site=$2 [QSA]
Result: No Error, but "site" has only a param at (1.).
RewriteEngine On
RewriteRule ^(.*[/]){0,1}(.*).html$ index.php/?lang=$1&site=$2 [QSA]
Result: Works fine, but i.e. lang="en/", should be lang="en"
In which way I can improve it and let it work correct?!
Thanks a lot!!
Try this:
RewriteEngine On
RewriteRule (/en/)?([^/]+)\.html$ index.php/?site=$2lang=$1 [QSA]
No need to escape forward slashes in mod-rewrite, by the way. But you do need to escape the ., which, as you know, stands in as a wildcard in regular expressions.
I'm not an expert in url rewriting, but i like koalas:
RewriteRule /+(([^/]+)/)?([^/]+)\.html$ /index.php/?site=$3&lang=$2 [QSA]