Variable in .htaccess file - .htaccess

i want to redirect my pages whics are ending with the number.
for example:
www.example.com/content/1213 => www.example.com/content/sport/1213
thats ok; but i have pages, ending with the words too at the same folder ( www.example.com/content/xxxx )
.htaccess musnt do something to these pages.
i mean:
www.example.com/content/1213 => www.example.com/content/sport/1213 OK
www.example.com/content/xxx => DO NOTHING
Can i do it with htaccess?

You can put this code in your htaccess (which has to be in your document root folder)
RewriteEngine On
RewriteRule ^(content)/([0-9]+)$ /$1/sport/$2 [R=301,L]
Note: don't forget to enable mod_rewrite

Related

How to direct http://localhost/DM/index/fb1ffc41 to http://localhost/DM/fb1ffc41

How can redirect localhost/DM/index/fb1ffc41 to localhost/DM/fb1ffc41 via .htaccess file.
fb1ffc41 this code is for short URL similar like Google short URL service.
Try the following at the top of the .htaccess file in the document root:
RewriteEngine On
RewriteRule ^DM/index/([a-f0-9]+)$ /DM/$1 [R,L]
This assumes the "short code" is 1 or more hexadecimal digits.
This is a temporary (302) redirect.

remove directory after main url using htaccess

With the following url http://www.example.com/de/here/ I want to remove the "de" directory (or whatever may be in front of the "here" directory, if anything even is in front of it) so a user is directed to http://www.example.com/here/ instead, which is a directory that does actually exist.
The url could even be something like http://www.example.com/it/here/ or any other combination of 2 letters.
The url could also just be http://www.example.com/here/ in which case I don't want anything removed at all.
I have searched for a solution here but cant seem to make anything work correctly so any help would be much appreciated.
You can use this kind of htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/(.*)$ $1 [L,R=301]
Example of redirections caused by this code :
http://www.example.com/de/foo/ => http://www.example.com/foo/
http://www.example.com/de/ => http://www.example.com/
http://www.example.com/it/bar/ => http://www.example.com/bar/
http://www.example.com/FR/baz/ => http://www.example.com/baz/
Please note you won't be able to access the language (de, it, fr...) anymore.
Another point, be careful with this kind of url (the redirection will be executed twice) :
http://www.example.com/de/go/ => http://www.example.com/go/
http://www.example.com/go/ => http://www.example.com/
EDIT
Now I've got more details, here is an htaccess you can you to remove the language for specified folders :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/here/(.*)$ here/$1 [L,R=301]
RewriteRule ^[A-Za-z]{2}/anotherfolder/(.*)$ anotherfolder/$1 [L,R=301]

How to 301 redirect pages "up" a page

I'm a newbie and I'm trying to figure out the proper 301 redirect for the following pages. I hope I'm being clear here :) In my .htaccess file, I want to redirect pages "up" one pages without having to do every page individually.
My original pages looked like the following:
www.doctors.com/skin/california/best-skin-doctors-california/
www.doctors.com/skin/california/best-skin-doctors-california/?page=1
www.doctors.com/skin/california/best-skin-doctors-california/?page=2
....etc. ....up to like /?page=33
and more categories and states, like:
www.doctors.com/heart/new-york/best-heart-doctors-new-york/
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=1
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=2
...etc. .....again up to like /?page=24
I've since changed the page structure to eliminate the long URLs...like this:
www.doctors.com/skin/california/
www.doctors.com/skin/california/?page=1
www.doctors.com/skin/california/?page=2
etc.....and similarly....
www.doctors.com/heart/new-york/
www.doctors.com/heart/new-york/?page=1
www.doctors.com/heart/new-york/?page=2
etc.
How can I "bulk" redirect the original pages with the long URLs to the newer, shortened version in my .htaccess file? Thank you very much for your time and consideration!
Using mod_alias, you can simply add this in the .htaccess file in your document root:
RedirectMatch 301 ^/([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/
But if you need further restrictions on how the redirect works, you can use Apache's mod_rewrite module. Taking a look at the RewriteCond directive, you can impose conditions on a rule and put everything in .htaccess. The main rule will look very similar to mod_alias' RedirectMatch. Example:
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
In both cases, the query string (the page=3 part) is simply appended to the new target. Looking over the different things you can do with RewriteCond, say if you wanted to exclude this rule when requests are made for something like /images/ or /themes/:
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/themes/
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
So, if the request doesn't start with /images/ and the request doesn't start with /themes/, then apply the rule. This example would make it so a request for http://host.com/themes/subSilver/magic-icons/ don't get redirected to http://host.com/themes/subSilver/.

.htaccess modification

I am using direct paths for downloading files from my site. the link is something like this
http://www.site.com/download.php?dir1/dir/dir3/file.doc
i want to wrap it with mod rewite rules so that only below link should be appeared
http://www.site.com/download
file, dir and dir3 are variable.
what i'hv to do in my .htaccess file?? Any Idea??
A simple redirect would be:
RewriteRule ^http://www.site.com/download/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1 [NC,L]
This will take any request for something in the 'artificial' download directory and route it to the real location.
You can add more complex rules stripping out filetypes etc depending on your needs, or redirecting a 'name' to a filename etc etc..
e.g:
RewriteRule ^http://www.site.com/download/pdf/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1.pdf [L,NC]
This would have an artificial PDF folder containing a filename ex the extension, routing to a .pdf doc....you can shape the redirect any way you like really...depends on the format you prefer
Not specific question. What is dir1/dir/dir3/file.doc means? If you want to get http://www.site.com/download.php?dir1/dir/dir3/file.doc, when you go to http://www.site.com/download do the next things in your .htaccess file.
RewriteEngine on
RewriteRule ^download/(.*)/?$ download.php?dir1/dir/dir3/file.doc [L]

Htaccess rewrite if address is subfolder?

I have the following URLs:
www.mydomain.com/client
www.mydomain.com/client/index.php
www.mydomain.com/client/index.php?a=b
www.mydomain.com/client/index.php?a=b&b=c
The following two htaccess files exist:
www.mydomain.com/.htaccess
www.mydomain.com/client/.htaccess
I want to edit "www.mydomain.com/client/.htaccess" so that if you go to www.mydomain.com/client, that it redirects the user to mydomain.com/client/clientarea.php. In other words, 1 and 2 must redirect to mydomain.com/client/clientarea.php, but 3 and 4 must not.
How do I do that?
Try this:
RewriteEngine on
RewriteBase /
RewriteRule ^client$ /clientarea.php [L]
RewriteRule ^client/index.php$ /clientarea.php [L]
htaccess files are parsed in the order they're discovered, so the top level one (mydomain.com/.htaccess) will be parsed and executed before Apache even considers looking down in the .../client sub-directory. As such, you'd have to modify your rewrite rule to check if the request contains a subdirectory, and NOT process it if one's found.

Resources