I'm trying to rewrite url's with htaccess. The request url length is variable.
It consists of the following:
http://[domain.com]/[countrycode]/[main-category]/[sub-category]/[sub-sub-category]/[product-name].html
Of course it could also have less categories.
Another option is only a product page, like:
http://[domain.com]/[countrycode]/[product-name].html
I want to rewrite these url's and add a static value between the [countrycode] and the rest of the url path. Also, I want to change .html to .aspx at the end of every rewritten URL.
I came as far as being able to rewrite a url with only the main category like:
http://example.com/en/main-category with this htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_PORT} ^80$
RewriteCond %{HTTP_HOST} ^(.*)example\.com [NC]
RewriteRule ^(.*)/(.*)$ https://example.com/$1/3/$2.aspx [R=301]
But if the url contains a trailing slash or more categories, the static value ("3") gets added in the wrong place.
to be clear, I want this structure:
request: http://example.com/en/main-category/sub-category/product-name.html
rewritten: https://example.com/en/3/main-category/sub-category/product-name.aspx
Thanks for your help!
You may use this rule in site root .htaccess:
RewriteEngine On
RewriteRule ^([a-z]{2})/(?!3/)(.+)\.html$ https://%{HTTP_HOST}/$1/3/$2.aspx [L,NC,NE,R=301]
Related
I need to add redirects to .htaccess.
If the URL address does not contain /site/ or /builder/, redirect to /site/
Example:
http://www.mh.cz/test.php -> http://www.mh.cz/site/test.php
http://www.mh.cz/builder/a.php -> http://www.mh.cz/builder/a.php
http://www.mh.cz/site/contact.php -> http://www.mh.cz/site/contact.php
Actualy .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mh.cz [NC]
RewriteRule ^(.*)$ http://mh.cz/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mh\.cz$ [NC]
RewriteCond %{REQUEST_URI} !/site/
RewriteRule ^(.*)$ http://mh.cz/site/$1 [L,R]
How to modify the condition to redirect be ignored if the URL already exists /builder/ ?
Thanks
Using .htaccess redirect all pages to index.php or whatever you want.
In index.php, grab the requested URL and make conditions to determine which page to include depending on the URL.
For example, http://www.example.com/test.php will display content of http://www.example.com/site/test.php but in the address bar http://www.example.com/test.php will remain, the user will not get Error 404.
I'm not sure if this will fulfill your actual need but it will work.
My site was hacked not too long ago and a lot of URLs were created in the following format:
http://example.com/prematurely.asp?skin=pspfsffdproblems=nq....
is there anyway I can re-direct all these wildcard URLs based on prematurely.asp to a single page? for example:
http://example.com/newpage
.htaccess file is as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nowcosmetic\.co.uk$ [NC]
RewriteRule ^(.*)$ http://nowcosmetic.co.uk/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/questions-and-answers\.html$
RewriteRule .* http://nowcosmetic.co.uk/botox.html [R=301,L]
RewriteRule ^prematurely\.asp /botox? [R=301,L]'
You can do this by adding a rewrite rule
RewriteEngine on
RewriteRule ^prematurely/(.*) /newpage [R=301,L]
As mentioned in comments, if these URLs are the result of a hacked site which has now been resolved then you are better off (SEO wise) serving a 404 (or 410 Gone) instead of redirecting.
However, if you still want to redirect...
re-direct all these wildcard URLs based on prematurely.asp to a single page
At the top of your .htaccess file (in your document root):
RewriteEngine on
RewriteRule ^prematurely\.asp /newpage? [R=301,L]
Note that RewriteEngine On should only appear once in your file (at the top).
This simply matches against "prematurely.asp" (ignoring the query string - as per your request). The trailing ? on the RewriteRule substitution strips the original query string from the rewritten URL.
I want to change alle dynamic url's to static ones,
but after rewriting the dynamic url's are still responding/available.
What did I do =>
I found this Tool for SEO:
http://www.webconfs.com/url-rewriting-tool.php
I entered this:
.../filmdetails.html?var=ich_einfach_unverbesserlich_ii
Then I put into my .htaccess this:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1
Works, but now I got a problem: This URL is still available and should not be: .../filmdetails.html?var=ich_einfach_unverbesserlich_ii
How do I get rid of the dynamic url's?
Your rule only rewrites the nicer looking URL to the one with a query string. Rules only work from a "pattern" -> "target" way, the mapping won't magically work the other way. You'll have to create a separate rule in order to redirect the browser:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /filmdetails\.html\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /filmdetails/%2/?%3 [L,R=301]
Try:
Options +FollowSymLinks
RewriteEngine On
#set this to path to your filmdetails.html file (from the document root)
RewriteBase /
#checking if redirect already happened
RewriteCond %{ENV:REDIRECT_PASSED} !^$
RewriteRule $ - [L]
#Your rewrite rule
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1 [L,E=PASSED:1]
#redirecting from filmdetails.html with query string ?var=something
RewriteCond %{QUERY_STRING} ^var=(.+)$ [NC]
RewriteRule ^filmdetails.html$ filmdetails/%1/? [R]
filmdetails.html?var=something will be redirected to filmdetails/something
Can anyone help with .htaccess URL rewriting rule.
I need to redirect pages like this:
www.website.com/index.php?a=some-word-some-other-word
www.website.com/index.php?a=something
www.website.com/index.php?a=some-other-thing
All that pages have the same content as www.website.com/index.php
to
www.website.com/index.php or www.website.com/
I already asked for this and I got this:
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .* http://www.website.com/? [R=301,L]
It does that, it redirect those pages to home page (root of site). But I have some pages with URLs like:
i.php?a=something-something-something
and problem is that .htaccess code above affect those links too.
Can anyone make that code just to redirect links for pages based on index.php?a= and not for i.php?a= too.
Try this and tell me if it works:
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule ^index\.php$ http://www.website.com/? [R=301,L]
Hi i make one cms sites and i need to rewrite my url
current my url is http://www.example.com/index.php?link=pages&cmsid=2&cmsLink=Carpet
it refers the cmsLink
I want my url like http://www.example.com/Carpet
I am using the following code
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^index.php?link=(.*)&cmsid=(.*)&cmsLink=(.*) $3
To achive this url it not directly possible with .htaccess
I use regular expression and other tings in htacess as well
I put remove the cmsid
current my url is http://www.example.com/index.php?link=pages&cmsLink=Carpet
RewriteCond %{REQUEST_URI} !/admin
RewriteCond %{REQUEST_URI} !^/(.*).php
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?link=pages&cmsLink=$1&%{QUERY_STRING} [L]
it return me http://www.example.com/Carpet
Try changing your last rule to this:
RewriteRule ^(.+)$ /index.php?link=pages&cmsid=2&cmsLink=$1
Since you want to have url like http://www.example.com/Carpet, so cmsid and link in your url has to be hardcoded to 2 and pages.