Rewrite rule causing a not found error - .htaccess

At the moment, when I visit example.com/index.php?page_id=6726, the desired page is displayed. I would like to rewrite the URL to the format of example.com/newsletter.
My current .htaccess file includes this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com\/newsletter [NC]
RewriteRule ^(.*)$ index.php?page_id=6726 [NC,QSA]
Yet, I receive a page not found error.
How can I make the rewrite work so that when I visit example.com/newsletter the content of example.com/index.php?page_id=6726 is displayed?

RewriteEngine On
RewriteRule ^newsletter$ index.php?page_id=6272 [L,NC]
More generally, if the page_id is a variable, so
RewriteRule ^newsletter/?(\d+)?$ index.php?page_id=$1 [L,NC]
will redirect the visitor to many similar pages, such as
example.com/newsletter/6272 -> example.com/index.php?page_id=6272
example.com/newsletter/272 -> example.com/index.php?page_id=272
example.com/newsletter/627 -> example.com/index.php?page_id=627
...

Related

condition modification in .htaccess

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.

htaccess not redirecting correctly

I am trying to redirect to a new domain, but it isn't retaining the complete url on the redirect, it only goes to the homepage. Here is the line in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.old-domain\.com
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=permanent,L]
For example, if I go to www.old-domain.com/contact-us/request-a-demo/, it should go to www.new-domain.com/contact-us/request-a-demo/
Instead, anything I type after the first "/" is redirecting to the homepage for www.new-domain.com
What am I missing, please help
Maybe you could try this:
RewriteEngine On
RewriteCond %{SERVER_NAME} ^(.+\.)?old-domain\.com
RewriteRule ^/?(.*)$ http://www.new-domain.com/$1 [R=permanent,L]

Redirect url without changing the url

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]

.htaccess mod_rewrite won't skip RewriteRule with [S]

As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php
First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps
Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]

301 redirect from query string to simple URL

I've had a good look through the first ten pages of search results for "301 redirects" and can't find the answer so here goes...
I've moved from a crappy old CMS that didn't give my pages nice URLs to one that does and I want to set up a 301 redirect for my key pages.
Current URL: http://www.domain.com/?pid=22
New URL: http://www.domain.com/contact/
I'm using Wordpress and my current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Any help would be awesome!
Give this a try. All you need to do is check to see if you are on page X and then redirect to page Y. Consider RewriteCond statements to be 'if' statements. If you need more redirects just duplicate the last two lines and edit the paths.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.domain\.com\/?pid=22$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/contact [L,R=301]
You have to check the query string for the value in the "pid" variable and then redirect if the value in that variable matches a page you want to redirect. You can do this with the "RewriteCond" and "RewriteRule" directives like this:
RewriteEngine On
RewriteBase /
# Redirect pid=22 to http://www.domain.com/contact
RewriteCond %{QUERY_STRING} ^pid=22$
RewriteRule ^(.*)$ http://www.domain.com/contact [R=301,L]
You can repeat the "RewriteCond" and "RewriteRule" directives to create additional redirects.

Resources