Redirect url without changing the url - .htaccess

I know there are a lot of answers available for this but none of them worked for me. I am stuck with this very familiar issue. I have made a website wherein i have to make customised urls for all my franchisees. But all these customised urls should take back to homepage.
Eg. if franchisee enters following url : www.example.com/franchisee/john it should redirect the franchisee to www.example.com but the browser url should remain www.example.com/franchisee/john
I have tried this by modifying .htaccess file but it shows page not found(404) error. Any help will be appreciable. I am new to .htaccess.
Here is .htaccess code :
RewriteCond %{REQUEST_URI} ^.*/franchisee/[a-zA-Z0-9_\.\-]+$
RewriteRule ^(.*)$ / [P]
EDIT :
Here is the complete .htaccess file. This file is present under the docroot folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
Options +FollowSymLinks -MultiViews
RewriteCond %{REQUEST_URI} ^/franchisee/[a-zA-Z0-9_\.\-]+$
RewriteRule ^franchisee/[\w.-]+/?$ / [L]
</IfModule>

You don't need to use proxy or P flag here. Use this rule without R flag to silently rewrite to /:
RewriteEngine On
RewriteRule ^franchisee/[\w.-]+/?$ / [L]

Related

Seo friendly url - Unable to find a way to get the htaccess to rewrite the urls

Currently, my site has this url format:
https://example.com/blog/news.php?read=article-title-here
I would like to change this url to:
https://example.com/news/article-title-here
Any idea how to do this please? I tried this method but see no changes in the browser bar(even after emptying the cache):
RewriteCond %{QUERY_STRING} ^read=(.*)$
RewriteRule ^news/?$ %1.php? [R=301,L]
Also, where should the htaccess file be written?
in https://example.com/
or
https://example.com/blog/
Thank you in advance!
Ben
You can use in your https://example.com/.htaccess:
Options -MultiViews
RewriteEngine on
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+blog/news\.php\?read=([^\s&]+) [NC]
RewriteRule ^ /news/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^news/(.+?)/?$ blog/news.php?read=$1 [L,QSA,NC]

Htaccess Trouble redirecting wildcard.domain.com to domain.com/wildcard

I am having trouble with an htaccess setup. My goal is to have a wildcard sub directory point to the appropriate folder but without changing the URL. I followed some examples here on stackoverflow and elsewhere. I already have my wildcard setup in DNS, Virtual Host is good to go and mod packages. All that is left is the htaccess pointing to the matching folder without changing the url in the browser.
This Works as in I see the domain.com/example/index.html file (But the url changes from example.domain.com to domain.com/example in browser)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [L,NC,QSA]
</IfModule>
When I swap out the L flag for the P flag so that the url doesnt change. When I do and I try example.domain.com the url does not change as intended but it shows me what I would see if I had went to domain.com instead of what is in the /example folder.
Here is what I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1/$1 [P,NC,QSA]
</IfModule>
What am I doing wrong?
You are trying to provide the URL in your substitution, which causes the redirection. Try to simply set config as follows:
In VirtualHost
ServerName example.com
ServerAlias *.example.com
DocumentRoot /path/to/your/doc/root
In htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1 [NC]
# Please confirm whether you need the next line in your rules.
# I should think that it'd be required
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L,QSA]

Internal Redirect blog/article.php?id=1 to blog/article/1

I have a blog folder in the public_html folder on my server with godaddy.
The .htaccess (in public_html) :
RewriteEngine on
Options +FollowSymlinks -Multiviews -Indexes
RewriteBase /
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} /blog/article\.php\?id=([^\s&]+) [NC]
RewriteRule ^ blog/article/%1? [R=302,L]
RewriteRule ^blog/article/([^/.]+)/?$ /blog/article.php?id=$1 [L,QSA,NC]
The page takes me to 404 page on my online host with godaddy but it works fine on localhost.
I also tried:
RewriteCond %{QUERY_STRING} (^|&)article\.php\?id=(.*)(&|$)
RewriteRule ^(.*)/article.php$ $1/article/%1? [NC,L]
but that didn't do anything.
Please help!
Figured it out. For anyone that might run into the same problem, here is the simple solution:
Make sure that your tag contains the real url.
What I did was, I put the custom url in the tag and godaddy was not recognizing it.
So what I did was
article 1
instead it should be
article 1
That way it will redirect to the custom url correctly.
Hope that helps!

Domain name within domain name. How to redirect through htaccess

The crawling properties of site showing
http://www.abc.com/http://www.abc.com/index.php?option=com_toys
http://www.abc.com/http://www.abc.com/index.php?option=com_article
http://www.abc.com/http://www.abc.com/index.php?option=com_play&view=category&vid=10
The site been crawled like this, with errors coming in as duplicate url. Correct url is
http://www.abc.com/index.php?option=com_toys
http://www.abc.com/index.php?option=com_article
http://www.abc.com/index.php?option=com_play&view=category&vid=10
is there any way to 301 redirect
i tried using
RewriteCond %{REQUEST_URI} ^.*/http://www.abc.com.*$
RewriteRule .* index.php [R=301,L]
But its not achieving the desired as redirecting to http://www.abc.com/index.php
How to redirect through htaccess from incorrect url to correct url sttructure off site
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+http://.+?(/index\.php\?[^\s]+) [NC]
RewriteRule ^ %1 [R=301,L,NE]
Try this code :
RewriteEngine On
RewriteRule ^http://www.abc.com/index.php /index.php [QSA, R=301]

.htaccess: rewrite condition and rewrite problems

I am working on a CakePHP project in shared hosting with multiple subdomains. Due to problems with Cake's htaccess I had to move the main site into a subfolder, and write a new htaccess to redirect users to this folder (while leaving the subdomains requests intact). At the minute my htaccess file looks something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteRule (.*) /domain/$1
</IfModule>
This works for requests with 'www' prepended to the url, but there are some issues with http: // domain.com requests. In IE & Chrome this address resolves itself to the 'www' url, but in Firefox & Safari, it shows the directory structure.
I need to figure out how to include the http: // domain.com requests in the rewrite conditions without affecting the other sub-domains.
Any advice would be greatly appreciated. Thanks. Adrian
Change your %{HTTP_HOST} rewrite condition as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain/ [NC]
RewriteRule (.*) /domain/$1 [L]
</IfModule>
The ? in (www\.)? says zero or one occurrence i.e. makes it optional.
[L] marks it as last i.e. rewriting should stop at this rule.
[NC] makes all matches case-insensitive.
Try this for the host match:
^(www\.)?domain\.com$

Resources