Iam using apache mod_rewrite to redirect some URL. I been able to some of the URLS but none of my rules are able to caprure this
http://192.23.70.11/mome/GIS%20Endo/%2Fhome%2FGIS%2520Endo%2F
and redirect it to
http://192.23.70.11/mome/GIS%20Endo/
You can use this rule as your first rule in site root .htaccess or Apache config:
# this goes in httpd.conf
AllowEncodedSlashes On
RewriteEngine On
RewriteRule ^(/?mome/[^/]+)/.+$ /$1 [L,NC,R=301,NE]
This will match any URI starting with /mome and will truncate anything after 2nd path component.
Related
I want to redirect users from www.example.com/ANYTHING to www.example.com.
Is .htaccess the better way to do it? How can I do it?
If you really just have to remove everything after www.mydomain.com, then you just have to delete everything from the first / to the end of the URL:
Search for this regex
%^([^/]*).*$
and substitute with \1, as I did here. Note that I have used the % sign as delimiter instead of /, so I don't need to escape the / in the regex. (I could have used any other available symbol other than /.)
You'll need to use a mod_rewrite RewriteRule (as opposed to a mod-alias RedirectMatch) in order to avoid conflicts with mod_dir and the DirectoryIndex*1.
For example, in .htaccess (Apache 2.2 and 2.4):
RewriteEngine On
RewriteRule . / [R,L]
The single dot matches something (ie. not the document root) and we redirect to the document root.
However, if the document root is an HTML webpage that links to resources like JavaScript, CSS and images then you need to make exceptions for these resources, otherwise these too will be redirected to the root!
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|jpg|png|gif)$ [NC]
RewriteRule . / [R,L]
*1 A mod_alias RedirectMatch directive such as RedirectMatch /. / ends up matching the rewritten request (by mod_dir) to the DirectoryIndex (eg. index.php) resulting in a redirect loop.
This URL
http://www.example.com/news/2138-gideon-rothschild-listed-leading-lawyer-expert-guides
needs to be redirected to this URL
http://www.example.com/news/gideon-rothschild-listed-leading-lawyer-expert-guides.
Here is another example:
http://www.example.com/articles/2140-can-you-trust-your-trust
to
http://www.example.com/articles/can-you-trust-your-trust
The client wants the page id removed from the URL. Can this redirect be done in the htaccess file or do I need to do it using PHP?
Yes, it can be done using a RedirectMatch rule.
Place this rule in your site root .htaccess:
RedirectMatch 301 ^/(articles)/\d+-(.+)$ /$1/$2
The very basic rule could look like the following:
RewriteRule ^([0-9-]+)(.*)$ /$2 [L,R=301]
You would need to fine-tune it a bit if you want only news/ or articles/
I have a quite simple rewrite rule, it works meaning the rewrite works but the parameter does not reach the destination page.
Each page has a section, title and a id.
The url has dashes in the name and I use a underscore separator to separate the id.
The section name is hypnose
The title is Hoe-werkt-hypnose
The id is 4
The rewrite rule:
RewriteRule ^hypnose/([^/.]+)_([^/.]+).php$ hypnose.php?title=$1&id=$2 [L]
The url that i'm using
hypnose/Hoe-werkt-hypnose_4.php
You need to turn MultiViews option off:
Options -MultiViews
RewriteEngine On
RewriteRule ^hypnose/([^/_]+)_([^/.]+)\.php$ hypnose.php?title=$1&id=$2 [L,QSA,NC]
Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
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 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