I trying to create a SEO friendly url in the format like this
http://web_url/news/**news_title**/**newsid**
which is redirected to
http://web_url/index.php?t=news&newsid=newsid
I wrote the following .htaccess rule but it fails as long as the url format has no special characters but once a special character is present in the url, it fails
eg. http://web_url/news/mywebsite.com is live!/4
Here are my rules:
RewriteRule ^news/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?t=news&newsid=$2
RewriteRule ^news/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?t=news&newsid=$2
Any help would be greatly appreciated
Just use
RewriteRule ^news/([^/]+)/([0-9]+)/?$ index.php?t=news&newsid=$2
instead of both rules.
Related
If any user submit the ! character as input value I want to redirect the URL to the home page.
example.com/index.php?c%5B%5D=%21&c%5B%5D=&c%5B%5D=&c%5B%5D=&c%5B%5D=&ic=&submit=find
If multiple input values are submitted with the character ! I want to be redirected to the main page else the code works on the same page.
For example, in this URL:
https://www.example.com/currencyconverter/convert/?Amount=!&From=USD&To=EUR
the value ! is contained so I want to redirect it to my page example.com. However, if the URL is like this:
https://www.example.com/currencyconverter/convert/?Amount=!3&From=USD&To=EUR
in this URL I submit input value !3 so I don't want to redirect it. The only thing I want is if the URL contains only ! value it is redirected to home page else code works normally. How can I do that?
If any URL parameter contains exactly ! only (URL encoded or not) then redirect to the document root. You can do this using mod_rewrite near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} \=(!|%21)(&|$)
RewriteRule ^ / [QSD,R=302,L]
UPDATE: The regex \=(!|\%21)(&|$) matches any URL parameter value where the value is either ! or %21 (! URL encoded) exactly. Whilst the = is not a special regex meta character in this context, it needs to be backslash-escaped to avoid being interpreted as the = Apache mod_rewrite prefix-operator. Alternatively, you could match a single character before the literal =, eg. .=.
The QSD flag is necessary to discard the query string from the redirect response.
Note that this rule currently applies to any URL-path. If it should only apply to /currencyconverter/convert/ (for instance) then be specific in the rule. For example:
:
RewriteRule ^currencyconverter/convert/$ / [QSD,R=302,L]
NB: The URL-path matched by the RewriteRule pattern does not start with a slash.
Aside:
But why not block the request with a 403 Forbidden, rather than issue a redirect? ie. use the following RewriteRule instead:
:
RewriteRule ^ - [F]
I'm currently trying to add some redirects from a very old site that had PDFs and some of these PDFs have used brackets to contain the years.
For example:
/document%2070C11-name%20(2012).pdf
So the full URL I am trying to redirect is:
https://website.co.uk/document/1/document%2070C11-name%20(2012).pdf
I have previously setup many different redirects on this site but ones with the special characters seem to be causing an issue.
Here is the examples of what I have tried, and failed with so far:
RewriteEngine on
RewriteBase /
RewriteRule ^document/1/document%2070C11-name%20\(2008\).pdf$ document/newurl/ [R=301,L]
RewriteRule ^document/1/document%2070C11-name%20(.*)2008(.*).pdf$ document/newurl/ [R=301,L]
So I tried escaping the character and just trying a wildcard. Neither seemed to work on my Apache server. The code I used did seem to work when I tested it on:
http://htaccess.madewithlove.be/
But I am pretty stuck now and would love any help I can get.
Thanks,
Kane
To match %MN character in URL, you need to use \xMN in the RewriteRule pattern.
Hence this rule should work for you:
RewriteRule ^document/1/document\x2070C11-name\x20\(2008\)\.pdf$ /document/newurl/ [R=301,NC,NE,L]
This will redirect https://website.co.uk/document/1/document%2070C11-name%20(2008).pdf to https://website.co.uk/document/newurl/
I am moving a site from IIS to Apache and part of the transition requires a change in URL handling.
The current URL pattern is:
http://example.com/articles/tabid/110/ID/1691/simple_friendly_name_of_page.aspx
1691 is the unique id of the post. It consists of two or more digits.
I would like to use .htaccess so as to ignore the "simple_friendly_name_of_page.aspx". Please bear in mind that the aforementioned aspx page can contain UTF-8 characters above the ASCII range (Hebrew, Greek etc)
In essence, I would like to permanently (301) redirect requests for
http://example.com/articles/tabid/110/ID/1691/simple_friendly_name_of_page.aspx
to
http://example.com/articles/tabid/110/ID/1691
I imagine that this has to do with
RedirectMatch 301 http://example.com/articles/tabid/110/ID/(.*)/(.*)
but I cannot grasp the regex needed for the other part of redirect / mod-rewrite.
Any help would be much appreciated.
Thanks in advance!
RewriteEngine on
RewriteRule ^articles/tabid/([0-9]+)/ID/([0-9]+)/(.*)$ articles/tabid/$1/ID/$2 [R=301]
or
RedirectMatch 301 ^/articles/tabid/([0-9]+)/ID/([0-9]+)/(.*)$ http://example.com/articles/tabid/$1/ID/$2
I am using url rewrite like this
RewriteRule ^help$ help.php
so if user come with domain.com/help it work
but if user call page like domain.com/help/ it send err 404 page not found
how can fix this that in 2 case it do same job
Try this rule:
RewriteRule ^help/?$ help`
Adding /? makes the trailing-slash to be optional. The pattern then starts to match help as well as help/
You can test this rule here
You need to make the trailing slash in your regular expression optional:
RewriteRule ^help/?$ help.php
I need this redirection with mod_rewrite, but I don't have any success.
The url is http://www.domain.com/descargar catálogo
I thought this would work:
RewriteRule ^descargar%20catálogo$ /url/whatever.php [NC,NE]
Any solution for this. I have tried with NE and without, etc...
This gives me internal error:
RewriteRule ^descargar catálogo$ /url/whatever.php [NC,NE]
Other things I tried but didn't work:
RewriteRule ^descargar%20cat%C3%A1logo /url/whatever.php
RewriteRule ^descargar\ cat%C3%A1logo /url/whatever.php
URLs cannot contain non-ASCII characters. Escaping the character to represent the actual way the URL is transmitted should do it:
cat%C3%A1logo