Forcing a trailing slash to a URL after a subdomain rewrite - php

I'm re-writing a subdomain to a 'folder' - actually a page in wordpress, and this all seems to be working correctly. I don't want the address in the URL bar to change though.
Everything works fine unless the user does not put a trailing slash after the page name, then the page is still redirected to the correct URL but the URL in the address bar changes
For example: foo.example.com/bar
Becomes: public.example.com/foo/bar
Where : foo.example.com/bar/ stays at the correct URL in the address bar but shows the redirected page (this is correct)
What rule do i need to add to add in a trailing slash if its not sent?
The code i have so far is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymlinks
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^(.*)$ http://public.example.com/foo/$1 [P]
# rules for WordPress ...
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#####
</IfModule>
Any help would be fantastic, I'm pretty new to htaccess. Thanks!

Phew, after a bit of playing around i seem to have got it working:
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://foo.example.com/$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^foo\.example\.com$ [NC]
RewriteRule ^(.*)$ http://public.example.com/foo/$1 [P]
Basically, the first block adds a trailing slash to the URL is it's not there in the first place, then the second block does the proxy redirect for the URL.
As far as i can see this catches all cases, but let me know if there are any gaping holes!

This rule should do it:
RewriteRule .*[^/]$ %{REQUEST_URI}/
Put this rule in front of your other rules. You also may want to add a condition to exclude existing files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/

Related

Php url rewrite and redirect in htacess

I want to redirect my site to friendly.
I have my website pull data from database and adress is /post?id=1
and i want to change it to /post/1
I already wrote the code for rewrite but i cant make sense from google research how to redirect to /post/1.
my code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L]
Use:
Options +FollowSymLinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /post/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L,QSA,NC]
This one worked for me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ post.php?id=$1 [NC]
if you goto url /post/check1/
the htaccess file internally call post?id=check1

PHP Mod Rewrite

I have a .htaccess which does a basic rewrite which looks like the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]
The next condition is to remove www. from the URL and looks like this:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
The problem is, when I call a link such as:
http://somesite.com/category/subcategory/?id=123
and add the www. manually, it rewrites the URL to this:
http://www.somesite.com?url=category/subcategory/?123
The page stills load but, I've been told that's terrible for SEO. Any thoughts of how to fix this?
Your current issue is that you have your WWW redirect after your main SEO rules, this is what happens behind the scenes:
You access http://somesite.com/category/subcategory/?id=123
Your rules internally redirect it to index.php?url=category/subcategory/?id=123
Your last rule to redirect without the www will also take place and will ended up redirecting it to:
http://somesite.com/?url=category/subcategory/?id=123
In order to fix that you would need your rules as follow:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?url=$1 [L,QSA]
Which leads us to your second issue, you've been using 301 redirects so your browser have cached some redirects and you will need to use a different browser temporarily to test your changes, preferable one you haven't used yet to access that site, while you clear the cache of that browser and wait for it to completely clear out.
Once the cache of your default browser has cleared you can use it as usual and you should get the same response.

Passing a query string through redirect for maintenance mode

I am looking for some guidance when working with a maintenance mode system, utilised within a .htaccess file, via mod_rewrite and passing a $_GET along with it.
Passing a $_GET in mod_rewrite is perfectly fine under normal circumstances for me, but in this instance I am experiencing issues..
The code I have for the maintenance system is as follows:
RewriteCond %{REMOTE_HOST} !^XX\.XXX\.XXX\.XXX
RewriteCond %{REQUEST_URI} !/maintenance.php$
RewriteCond %{REQUEST_URI} !^/css/.*$
RewriteCond %{REQUEST_URI} !^/assets/.*$
RewriteRule ^$ maintenance.php [R=302,L]
So what I need is the ability to pass a $_GET along with the rewriteRule, thus when the site is viewed as normal by anyone using the allowed IP, I can define that the site is being viewed in maintenance mode.
Of course people not on the allowed IP get redirected to maintenance.php file and don't need this reminder anyway, as the page does that already.
Thank you in advance anyone that can help me in this issue.
EDIT::
# Start the mod re-write conditions #
RewriteRule ^([^/]+)\/$ ?cat=generic&page=$1 [L]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$ ?cat=product&page=$2 [L]
RewriteRule ^theteam\/([^/]+)\/$ ?cat=staff&page=$1 [L]
Thats how I deal with the other links on my page, I hope that is all you needed to see.
Dan.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !maintenance.php
RewriteCond %{REQUEST_URI} !(css|assets).*$
RewriteCond %{REMOTE_ADDR} !^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) /maintenance.php [R=302,L]
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) $1?mode=maintenance [L,QSA]
This works for me, first you have the negation redirect so whoever tries to access without the allowed IP will go to maintenance.php.
Then you have the append internal redirect, the GET mode=maintenance will not be visible but will be there.
And you can retrieve it with $_GET['mode'].
If you want it to visually append to the query string and only to php files you can use:
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{QUERY_STRING} !^mode=maintenance.*$
RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
Update of visible rule for your sub directories format this rule should be placed before it like this:
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{QUERY_STRING} !.*mode=maintenance.*$
# for visible query string uncomment the below line and comment the next rule
#RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
# for invisible query string
RewriteRule (.*) $1?mode=maintenance [L,QSA]
# Start the mod re-write conditions #
RewriteRule ^([^/]+)\/$ ?cat=generic&page=$1 [L,QSA]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$ ?cat=product&page=$2 [L,QSA]
RewriteRule ^theteam\/([^/]+)\/$ ?cat=staff&page=$1 [L,QSA]

SEO friendly URLs being appended?

I've built the following htaccess file from googling around and looking at different examples.
My file now consists of the following rules:
## Enable Rewrite Engine
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
## Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
## Actual URL re-writes
RewriteRule ^profile/(.*)/? profile.php?pid=$1 [QSA,L]
RewriteRule ^profile/(.*)/(.*)/? profile.php?pid=$1&view=$2 [QSA,L]
From what I understand, this will rewrite URL's such as http://www.domain.com to http://domain.com, disable listing of directory files (+FollowSymLinks), as well as append a forward slash to urls such as http://domain.com/test and change it to http://domain.com/test/ so there's only one representation of that URL in search engines. Awesome! But, my URL keeps appending the part of the url to be re-written when I click on a link in one of my HTML breadcrumbs...
Example: http:/domain.com/profile/1/shouts/pictures/all/all/all/all/all/ if I continuously click on the same link... Why is this happening?
This is issue is driving me absolutely insane!
I've tried reading mod rewrite cheatsheets and tutorials, but none of them seem to make any sense to me :(
Edit:
I should note that I'm echoing my a href code with (relative?) links. Is this the correct way of doing things?
All
Edit 2:
I just tried using absolute links too, but getting the same issue. My PHP now reads:
<a href="<?php echo $websiteUrl. "profile/". $p_id."/all" ?>">
Where $websiteUrl = http://domain.com/ Any ideas anyone?
Your code is almost right but some minor corrections are still needed. Pls try this code:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
## Rewrite URL to exclude wwww.
RewriteCond %{HTTP_HOST} ^www\.(domain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=302]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
## Add trailing slash to url
RewriteRule (?!^.*/$)^ %{REQUEST_URI}/ [L,R=302]
## Your URL re-writes
RewriteRule ^profile/([^/]+)/?$ /profile.php?pid=$1 [NC,L,QSA]
RewriteRule ^profile/([^/]+)/([^/]+)/?$ /profile.php?pid=$1&view=$2 [NC,L,QSA]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Send subdomain as post data

Having spent the past few hours looking for a solution, I thought I'd ask here.
I'm looking for a way to process links such as:
http://subdomain.domain.com/page/subpage/
so they are passed to the server as:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
which can be taken by index.php to display the correct page information.
I can get the page and subpage to work but have no clue how to get the subdomain. Note: if there is no subdomain, I would like that field to either be left blank or contain a default value.
Any assistance would be much appreciated. :)
Edit:
Just to clarify, I want the first URL to be what the user sees and can input into the address bar and the second URL to be the page that is loaded by the server when the user navigates to the first URL.
My .htaccess file
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# Remove 'www'
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://resolutiongaming.com/$1/ [L,R=301]
# Subdomain redirect
RewriteCond %{HTTP_HOST} ^resolutiongaming.com$
RewriteRule ^webdev/$ http://webdev.resolutiongaming.com [R=301,L]
RewriteRule ^artwork/$ http://artwork.resolutiongaming.com [R=301,L]
RewriteRule ^music/$ http://music.resolutiongaming.com [R=301,L]
ErrorDocument 404 /error.php
You may try this in one .htacces file in root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)/? [NC]
RewriteRule .* http://domain.com/index.php?subdomain=%1&page=%2&subpage=%3 [R=301,L]
It will redirect permanently:
http://subdomain.domain.com/page/subpage/ or http://www.subdomain.domain.com/page/subpage/
To:
http://domain.com/index.php?subdomain=subdomain&page=page&subpage=subpage
For the rule to work, the incoming URL scheme: /page/subpage/ must be kept.
To replace permanent redirection with silent mapping, remove R=301 from [R=301,L] or replace it with R for temporal redirection.

Categories