I need to make a specific page url to user friendly
I have a page www.example.com/index.php?route=a/b
Which I want to be rewritten as -> www.example.com/a
I used this rule in htacces but its not working
RewriteRule ^/a$ index.php?route=a/b
Please help
Change your rule to:
RewriteEngine On
RewriteRule ^a/?$ /index.php?route=a/b [L,NC,QSA]
And remember that inside .htaccess leading slash in URI is not matched so ^a/?$ instead of ^/a/?$.
Reference: Apache mod_rewrite Introduction
Related
Going to show my lack of regex knowledge here but seem to be having trouble with the following:
I need to redirect the following example structure of a url:
www.example.com/blog-title-here/tags/this+is+a+long+tag+name
www.example.com/tag/this-is-a-long-tag-name
I need to redirect to just /tag and replace any instance of + with a -
You can use this recursive redirect rule as your very first rule in site root .htaccess:
RewriteEngine On
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*[ +].*)$" $1-$2 [N,NC,DPI]
RewriteRule "(?:.*/)?(tag/[^ +]*)[ +]+([^ +]*)$" /$1-$2 [L,R=301,NC,NE]
This will redirect http://localhost/blog-title-here/tag/this+is+a+long+tag+name to http://localhost/tag/this-is-a-long-tag-name
What do I need to do to the following rewrite rule with slash in the url?
http://www.website.com/p/EQUITY+INVESTMENT+CORP%2FGA
to
http://www.website.com/test.php?name=EQUITY+INVESTMENT+CORP%2FGA
I tried the following way but not working.
RewriteRule ^p/(.*) /test.php?name=$1 [PT]
Your rule is fine but %2F isn't allowed in URIs by Apache. To allow %2F to be encoded to / you need to add:
AllowEncodedSlashes On
in your <VirtualHost...> section or in global context of your Apache config.
I'm trying to create a rewrite rule to redirect to a URL based on a conidtion.
I have had a look on-line for similar questions but am finding myself confused.
My rewrite rule looks like follows:
RewriteRule ^rates/(\w+)/?$ http://www.url.co.uk/rates/result.php?quotenum=$1 [R,L]
For example, this will redirect any combination of characters after the URL
www.url.co.uk/rates/1234
to
www.url.co.uk/rates/result.php?quotenum=1234
I am creating an admin page as well which will sit at the URL:
www.url.co.uk/rates/admin
How do I modify my rewrite rule to prevent the redirection of the URL to
www.url.co.uk/rates/result.php?quotenum=admin
When the URL is equal to
www.url.co.uk/rates/admin
This should work:
RewriteRule ^rates/((?!admin/?$)\w+)/?$ http://www.url.co.uk/rates/result.php?quotenum=$1 [R,L,NC]
(?!admin/?$) is a negative lookahead that will not match given regex if URI is /rates/admin OR /rates/admin
I've been trying to write a rewrite rule for apache to switch my gallery2 URLs to the gallery3 URL format:
Old url example linked: http://domain.com/gallery/photoalbumxyz/photo.jpg.html
New url example needed: http://domain.com/photos/photoalbumxyz/photo
Note that in the URL example above, "/photoalbumxyz/photo.jpg.html" is not an actual physical directory, it is just the way gallery2 rewrote "friendly" URLs. I can rewrite the /gallery/ to /photos/ by using a rule like the following:
RewriteRule ^(.*)$ /photos/$1 [QSA,L,R=301]
However I'm having trouble figuring out the matching and removal of the ".jpg.html" extension if it exists in combination with the /gallery/ -> /photos/ rewrite. The regex matching I believe is going to be .jpg.html to escape the periods, but how do I write rules to remove the ".jpg.html" extension and rewrite the directory?
RewriteRule ^\.jpg\.html$ $1 [NC]
RewriteRule ^(.*)$ /photos/$1 [QSA,L,R=301]
Edit:
Sorry! I neglected earlier to mention the album URL formats can change (doesn't have to specify a photo, and can include sub albums), I've added some specific examples:
The url rewrite rule also needs to follow:
old: http://example.com/gallery
new: http://example.com/photos
old: http://example.com/gallery/album
new: http://example.com/photos/album
old: http://example.com/gallery/album/subalbum/
new: http://example.com/photos/album/subalbum/
old: http://example.com/gallery/album/subalbum/photo.jpg.html
new: http://example.com/photos/album/subalbum/photo
Try:
RedirectMatch 301 ^/gallery(.*?)(\.(jpe?g|gif|png)\.html)?$ /photos$1
Or alternatively using mod_rewrite:
RewriteRule ^/?gallery/([^/]+)/([^.]+)\.(jpe?g|gif|png)\.html$ /photos/$1/$2 [L,R=301]
You don't need the QSA flag as query strings will automatically get appended if you don't have a ? in your rule's target.
I am planning to relaunch a website soon. Part of the relaunch is a switchover from Joomla! to Wordpress.
Besides some old pages, which will be redirected with some exact expression, I have a lot of pages, which will be redirected to the new root page.
Whats the right regular expression, if I want to redirect URLs like these:
http://www.somedomain.com/index.php?somevar=123&com_option=somevalue
http://www.somedomain.com/index.php?com_option=someothervalue
All the URLs have in common:
http://www.somedomain.com/index.php?
+
com_option
How can I redirect all those pages with one line of redirect code in .htaccess?
I assume you do not need somevar and only want to carry over com_option
RewriteEngine on
RewriteBase /
RewriteRule index.php(.*)com_option=(.*)$ http://newsite.com/index.php?com_option=$2 [L,R=301]
You will need to do something like this:
RedirectMatch 301 (.*)/the/old/domain/and/url/(.*) http://the/new/domain/and/path/$2
Now, that the site is gone live, I see that none of the answers to my questions worked out.
After some trial an error I found a solution:
By now Google offers a live-demo:
1) Go to google & search for "site:sqlxpert.de".
2) Click on one off the links having "option" in the URL.
3) You will be redirected to the new pages root - which in this case is the same domain and also an index.php, but the d*** QUERY_STRING is gone.
I needed a .htaccess snippet, which does the following (expressed in words):
If requested file is
INDEX.PHP or INDEX2.PHP
AND
QUERY STRING contains "option"
then 301 REDIRECT to
http://www.sqlxpert.de/
WITHOUT any QUERY STRING
The solution:
RewriteCond %{QUERY_STRING} option
RewriteRule ^index\.php http://www.sqlxpert.de/$1? [L,R=301]
RewriteRule ^index2\.php http://www.sqlxpert.de/$1? [L,R=301]