I have a map file which is working fine with SEO-friendly URLs
Now the customer want that the user should not be allowed to access the technical links means only SEO links should be accessable
e.g my map file looks like
buche-massiv-weiss-lackiert.html /?page=datail&productid=1212
at the moment SEO is working, but what I need is that If the user calls this
technical link i.e
/?page=datail&productid=1212
if should also be first redirected to SEO link and then the page should be called.
what will be the best way to do so?
In the same mapping file add a reverse entries like this:
/?page=datail&productid=1212 buche-massiv-weiss-lackiert.html
Then have this rule at top of your htaccess for redirection to pretty URL:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/*(/.*?)\sHTTP [NC]
RewriteCond %1::${mymap:%1} ^(.*)::.
RewriteRule .* ${mymap:%1}? [L,NE,R=301]
This is assuming name of your RewriteMap is mymap. Change it to whatever your map name is.
With Helicon Ape you could use reverse map on the same map file like this:
RewriteEngine On
RewriteMap map_rev txt_rev:my_map_file.txt [NC]
RewriteCond ${map_rev:$1|NOT_FOUND} (.*)
RewriteCond %1 !NOT_FOUND
RewriteRule (.+) %1 [R]
Then your existing map rule.
With ISAPI_Rewrite you will have to create another reversed map file and make sure contents of both files are synched. Usage is something like:
RewriteEngine On
RewriteMap map_rev txt:my_rev_map_file.txt [NC]
RewriteCond ${map_rev:$1|NOT_FOUND} (.*)
RewriteCond %1 !NOT_FOUND
RewriteRule (.+) %1 [R]
Related
I am new to WordPress and want to redirect some specific traffic. Here's an example -
A user from a different website clicks on a link with url http://mywordpresssite.com/redirect?url=http://amazon.in/blablabla
I want my website to redirect the request to
http://amazon.in/blablabla or whatever be the url in the querystring without loading anypage of my website.
My website should function normally for all other requests.
I tried with htaccess and came up with this -
RewriteCond %{QUERY_STRING} (?:^|&)url=([^&]+)(?:&|$) [NC]
RewriteRule ^(.*)$ %1 [R=302,L]
But the RewriteRule results in http://mywordpresssite.com/http://amazon.in/blablabla?url=http://amazon.in/blablabla which is wrong.
I am new to htaccess and would be grateful for any help.
Try the following in .htaccess in the root of your site. Note that this should go before your existing WordPress mod_rewrite rules but after RewriteEngine On.
RewriteCond %{QUERY_STRING} ^url=(.*)$
RewriteRule ^redirect$ %1 [R=302,QSD,L]
The %1 refers to the first parenthesised subpattern in the RewriteCond directive (ie. everything after the "url=").
The QSD flag (Apache 2.4+) removes the query string from the original request.
Before Apache 2.4 you would need to change the RewriteRule to read (note the additional ? at the end of the substitution):
RewriteRule ^redirect$ %1? [R=302,L]
I am using the following Rewrite rule
RewriteMap map txt:C:/seo/mapping.txt
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1} [NC,L,NS,QSA]
But there are some cases where the URL does not contains an extension
like user should be able to enter either /xyz.html or /xyzWhat I need is that if user don't enters an extension then in rewrite rule .html should be automatically appended.
Something like
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1\.html} [NC,L,NS,QSA]
I would separate these two cases like this:
RewriteMap map txt:C:/seo/mapping.txt
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1} [NC,L,NS,QSA]
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1.html|NOT_FOUND} !NOT_FOUND
RewriteRule ^([^/.]+)$ ${map:$1.html} [NC,L,NS,QSA]
I came up with this, which will capture the filename, and if there is an extension it will capture that too. The filename capture group then has .html appended to it. However I have not tested this with multiple / in it, but I'm sure you can adapt it as necessary.
Example on Regexr
RewriteRule ^\/(\w+)[.]?(\w)? $1.html [NC,L,NS,QSA]
I use the following rules to rewrite a subdomain call to a page in the root of the website:
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+)\.domain\.com$ [NC]
RewriteRule .? headlines.php?url=%1 [L,QSA]
This works fine. I use this for a rss - news related website. For example: http://economy.headlines.com will internally look at http://www.headlines.com/headlines.php?url=economy
I also want to link to the news items in the following way:
economy.headlines.com/news/title/id
How do i do this ? Because every time the first rules are "fired". Even if i make other rules with the [L] flag the other rules are fired and nothing happened.
How can i combine the rules above with new rules which also look at files in the root of the site but with parameters in the url ?
You should be able to combine both if you evaluate the path component of the request you rewrite:
Rewriteengine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9/_.-]+).domain.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ headlines.php?url=%1&category=$1&title=$2&id=$3 [L,QSA]
Probably you have to do some fine tuning, but I think you get the idea: the first argument of the RewriteRule is a regular expression that splits the request path into its components. Those can be referred to using the $1 and $2 notation you can see towards the end of the rule.
Edit: added the category parameter to the RewriteRule as discussed in the comments below.
I've wrote .htaccess file to convert urls to SEO friendly :
the original url is :
http://palestinianz.com/?page=person&p=Alex-Atalla
the content of .htaccess is :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
it should produce a link like this :
http://palestinianz.com/Alex-Atalla.html
But it makes no effect although I put the file in the root of my website !
where is the problem ?
If you want the URL to change in the address bar, you need to redirect the browser, then rewrite on the server. The first part of this answer explains the process that you want: https://stackoverflow.com/a/11711948/851273
So looking at the second list, the process is to redirect the browser if an ugly link is request to a nice looking link:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=person&p=([^\ ]+)
RewriteRule ^$ /%1.html? [R=301,L]
Now, on the server end you need to internall rewrite it back to the ugly url, which is more or less what you already have:
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
You can try:
RewriteCond %{REQUEST_URI} (.+)\.html$
RewriteRule ^(.+)\.html$ ./index.php?page=person&p=$1 [L]
Only problem with this is any .html page will go through this. May want to change to the following:
# URL: domain.com/person/Alex-Atalla.html
RewriteCond %{REQUEST_URI} (.+)/(.+)\.html$
RewriteRule ^(.+)/(.+)\.html$ ./index.php?page=$1&p=$2 [L]
This will allow you to have more robustness in changing the the page variable as well
I have a subdomain (store.example.com). Not only do I want to redirect traffic from store.example.com but I want to redirect it to specific files on the domain (everything I could find was based on redirecting the entire subdomain)
So I want store.example.com/Science-Fiction -> www.bundoranpress.com/category/1/Science-Fiction
How would I write this using htaccess file?
You will either need a couple of RewriteConds or (in my opinion the better approach) a php script handling your requests. For the first approach:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish) [NC]
RewriteRule (.*) http://www.bundoranpress.com/$1/$2 [L,R]
This will redirect store.example.com/ThanksForAllTheFish to www.bundoranpress.com/store/ThanksForAllTheFish selling Douglas Adam's books.
Now, you could go for adding all possible terms in the second RewriteCond like so:
RewriteCond %{REQUEST_URI} ^(ThanksForAllTheFish|Universe|Hitchhiking) [NC]
This would capture one of the terms and redirect to the corresponding address (e.g. www.bundoranpress.com/store/Universe). You see that this will be difficult very quickly.
A better approach would be to redirect all urls from a specific folder and subdomain to a php script handling the rest, like so:
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^books/(.*) [NC]
RewriteRule (.*) http://www.bundoranpress.com/index.php?sub=$1&cat=$2 [L,R]
This would redirect store.example.com/books/ThanksForAllTheFish to www.bundoranpress.com/index.php?sub=store&cat=ThanksForAllTheFish. You could access the variables via $_GET["sub"] and $_GET["cat"] respectively. This gives you a much greater flexibility at hand.