htaccess rule for remove begining part of the url - .htaccess

I need to hide beginning part of the url. For example
Url will be http://bookoffers.com?id=http://www.google.com
Where id will be another url. So I want to show only the value of the id. And hide "http://bookoffers.com?id=" this much part.

Try adding this rule on top of other rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?id=(https?://[^\s]+) [NC]
RewriteRule ^ %1 [R=301,L,NE]

Related

htaccess rewrite RegEx add static value between parameters

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]

Prevent redireting for a specific URL

I'm redirecting if the url contains a specific word.
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
Is it possible to avoid redirecting for a specific url that has the above specific word?
E.g.
The below gets redirect
http://www.exsample.com/forum/contactus.html?ypin9001234
to
http://www.exsample.com/homepage.html?no=ypin9001234
What I need is to continue the above redirect as it is but to avoid the redirect only for the below URL.
http://www.exsample.com/forum/members/gold/gold.html?ypin9001234
Note: The word gold is used only by this url.
You should use a RewriteCond with THE_REQUEST to match full URL with query string:
RewriteCond %{THE_REQUEST} !/forum/members/gold/gold\.html\?ypin9001234 [NC]
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
You can add a rewrite condition before your rewrite rule, e.g.:
RewriteCond %{REQUEST_URI} !^.*gold.*$
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
This will prevent any url containing the word gold from being rewritten. If you need to be more specific, just make the condition more precise:
RewriteCond %{REQUEST_URI} !^forum/members/gold.*$
RewriteRule ^forum(/.*)?$ /homepage.html?no=%{QUERY_STRING} [R=301,L,NE,NC]
etcetera...

.htaccess forcefully hide home page uri

I have the following .htaccess code:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^$ subpage [L]
So basically if someone visit "www.example.com," he'll see the content of "www.example.com/subpage" without the url changed. This is good.
However, they can still visit the page by "www.example.com/subpage." If that happens, I want the url changed back to "www.example.com."
Is it possible? What I've tried so far gave me redirection loop.
You need an additional rule that matches against the actual request and not the URI. Since the rewrite engine loops, the URI keeps changing, so you need to match against the %{THE_REQUEST} variable. You need this rule before the rule that you have in your question:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /+subpage(\?|\ |$)
RewriteRule ^ / [L,R=301]

.htaccess rewriterule for folder

I'm trying to redirect using the link: http://www.mysite.com/af/2 to http://www.mysite.com/af/2/, but I can not perform this procedure, the link /af/#### corresponds to a user ID and is a folder.
You can accomplish this using .htaccess?
A rule like the following will take care of that:
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [R=301,L]
The condition is to prevent urls that already end with a slash to match this rule. The rule will match everything ^ and redirect it to the same url with a slash behind it.

Apache Rewrite with variables in Query String

I have a two part problem that I have only been successful with the first part.
I have the following listed in .htaccess which works great:
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
So visitors can go straight to mysite.com/senior and the correct internal page (demo.php?dID=1) gets pulled up.
My problem is that I also would like a rewrite where /demo.php?dID=1 shows up in the URL bar as /senior. So existing links show up with the new user friendly url.
My attempt so far has been:
RewriteCond %{QUERY_STRING} ^dID=1$
RewriteRule ^demo.php$ senior [NC]
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
Thanks for your time and help.
You want to match against the request instead of the query string and redirect the browser:
RewriteCond %{THE_REQUEST} \ /demo\.php\?dID=1($|\ |&)
RewriteRule ^demo.php$ /senior? [NC,R]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Place this additional rule before your current rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+demo\.php\?dID=([^\s&]+) [NC]
RewriteRule ^ /senior? [R=302,L]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Your current rule based on QUERY_STRING will loop since internal rewrite rule will populate the QUERY_STRING and both rule will keep triggering each other.

Resources