Subdomain, htaccess Rewrite and masking URL - php

Users on my site are creating their own pages, i want to make subdomain with redirect and masking real URL.
I have made RewriteRule:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteCond %1 !=www
RewriteRule ^(.*)$ http://domain.com/site.php?site=%1 [QSA,R=302]
When user enters:subdomain.domain.com he will be trasfered to php script witch finds ID by page name in DB and redirects to:
domain.com/workarea/pages/ID/index.html
I wan't to mask real URL, just to show the URL with subdomain
subdomain.domain.com
but also i want to show all after / when user go to gallery or smth else, like:
subdomain.domain.com/index.html
subdomain.domain.com/gallery.html
but i don`t know how to do that.
I've tried to use [P] flag, but it just redirects to domain.com,
or it is just don`t let the php script to work.
Please help me!

First off, even if set up to have htaccess redirect a subdomain to php; the following remark:
When user enters:subdomain.domain.com he will be trasfered to php
script witch finds ID by page name in DB and redirects to:
domain.com/workarea/pages/ID/index.html
Will ensure that your visitors will see domain.com/workarea/pages/ID/index.html, because - as you say - php redirects visitors to that page. If you want to show the htaccess "masked" url, you need to make sure PHP parses the required HTML and not redirect.
As to the "masked" redirection from htaccess. What is the current behavior?

Related

URL Rewritng is working but auto redirect is not for my website

I have used URL rewriting with .htaccess with the below code. Problem is that if we paste the URL then it's working but auto redirection is not working.
.HTACCESS CODE:
Options +FollowSymLinks
RewriteEngine on
RewriteRule Sports-Equipment-Catname-(.*)\.html$ Sports-Equipment.php?Catname=$1
My URLS:
http://example.com/Sports-Equipment.php?Catname=Discus
If I paste direct URL from htaccess is:
http://example.com/Sports-Equipment-Catname-Discus.html
why URLs is not auto-redirected to new pages.
Please help me to fix it.
why URLs is not auto-redirected to new pages.
There is no "automatic" process here. You have no directives that implement the redirect.
My URLs: /Sports-Equipment.php?Catname=Discus
That's the problem, you need to change the actual URLs in your HTML source. This should not be resolved with a "redirect". Otherwise, the user can still "see" the old URL before they click the link. If they copy a link they are copying the "old" URL. Every time the user clicks a link they are redirected - which is slow, not just for the user, but also for your server (uses more resources).
However, you should implement a redirect if you are changing an existing URL structure. But this is to preserve SEO only. For search engines that have indexed the old URLs and for third party backlinks to the old URLs.
For that you would need to add the following "redirect" before the existing rewrites. For example:
# Redirect "old" URL to "new" URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^Catname=([^&]*)
RewriteRule ^Sports-Equipment\.php$ /Sports-Equipment-Catname-%1.html [QSD,R=301,L]
The condition that checks against the REDIRECT_STATUS env var ensures we only redirect direct requests and not rewritten requests by the later rewrite - thus avoiding a redirect loop.
The QSD flag discards the query string from the initial request.

How to retrieve the URL including the subdomain through PHP after htaccess rewrite rule?

I am currently using the code below to retrieve the URL of the current page that the user is on
PHP
$actual_link = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]."";
.htaccess
The problem with this is that I also have a htaccess rewrite rule which points all subdomains to their directory.
RewriteCond %{HTTP_HOST} ^(.*)\.example\.co [NC]
RewriteRule ^(.*)/?$ http://example.co/%1/$1 [P]
For example, if a user visits test.example.co, the URL will remain as that but the contents of example.co/test will be displayed instead.
As a result of this rewrite rule, if I use the PHP code above, it will return http://example.co/test rather than test.example.co.
How could I retrieve the exact URL that the user is on?
Overall
The exact URL cannot be retrieved due to a rewrite rule which points all subdomains to their directory
The path to the directory will be retrieved instead rather than the subdomain
How could I retrieve the actual URL that the user is on rather than the path it is pointed to?
Note - The URL bar still displays test.example.co but in fact it just displays the contents of example.co/test.

rewrite wordpress login url when it contains an affiliate ID

we are doing some affiliate tracking on a wordpress membership site with mod_rewrite. basically the URLS are domain.com/AFF_ID and AFF_ID is the members tracking id, this all works perfectly.
the problem comes in when members go to the affiliate tracked url domain.com/AFF_ID and login using domain.com/AFF_ID/wp-admin this generates a 500 error. rather than tell them not to use the affiliate id in the login URL i need to create a rule to remove everything before /wp-admin and the domain
basically
domain.com/could-be-anything/wp-admin
becomes
domain.com/wp-admin
i have tried various rules with no effect or i create an redirect loop. i would post what i have tried but they dont come close to working.
thanks for any suggestions,
Sean
You can try with this:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/wp-admin$ /wp-admin [L,NC]
It redirect any given path followed by /wp-admin to yourdomain.com/wp-admin
ex.
www.yourdomain.com/anything/wp-admin
redirect to:
www.yourdomain.com/wp-admin
i have not tried the above answer, looks like it may work with 1 exception, the registration url used wp-admin and we do want to track that page.
here is what i have that is working
RewriteEngine On
RewriteCond %{REQUEST_URI} ^.*/wp-admin
RewriteCond %{QUERY_STRING} !.*action=
RewriteRule ^.*/wp-admin /wp-admin [R=301,L]

Mobile Redirect using htaccess

My site is:
example.com
My mobile version:
m.example.com
Profile page is
example.com/profile or m.example.com/profile
Or posting page
example.com/posts or m.example.com/posts
Mobile version's url is similar. So, how to redirect the same page?
For example: user go (from facebook) into example.com/posts but he uses mobile device,
so how to redirect via .htaccess to m.example.com/posts
I saw this one threat, but i'm confuse to create right rules.
Thanks :)
Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.
Next, you want to go to your .htaccess file and add something like this:
RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]
The first RewriteCond contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond checks if the host isn't already m.example.com. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1 gets replaced with the first capture group in the regex in the first argument of RewriteRule. The [R] flag is not necessary in this case (links with http:// will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L] flag will stop processing more rules if this rule matches.
See the documentation for more information.

Multiple htaccess re-write rules with friendly URLs

My mobile site (tiny custom CMS inspired by wordpress) has a homepage, blog page, a news page, an about page and a contact page where ./blog ./news ./about and ./contact are actual folders. Homepage, Blog and News are paginated. About and Contact are single pages, no pagination.
I need htaccess rewrite rules to do the following (A) and (B). I have only made progress with (A), and the htaccess file is in the site root.
(A)
m.website.com/index.php?page=1 re-writes to m.website.com
m.website.com/index.php?page=2 re-writes to m.website.com/page/2 etc
m.website.com/blog/index.php?page=1 rewrites to m.website.com/blog
m.website.com/blog/index.php?page=2 re-writes to m.website.com/blog/page/2 etc
m.website.com/news/index.php?page=1 rewrites to m.website.com/news
m.website.com/news/index.php?page=2 re-writes to m.website.com/news/page/2 etc
Problems:
Below is what I'm using for the above, but I only got it working for the homepage for now. I don't know how to combine the rules to include blog and news pages too. Also, it duplicates my links because m.website.com and m.website.com/page/1 are both in use. I need to get rid of /page/1 everywhere. Pagination should start only from page 2. I tried to get rid of it using the RedirectMatch but it didn't work so I commented it out.
RewriteEngine On
RewriteBase /
RewriteRule ^page/(.*) index.php?page=$1 [NC]
#RedirectMatch 301 ^/page/1/$ http://m.website.com/
(B)
I already have a permalink.php file which accepts pretty URLs and returns their postIDs
The read-more link for each article on the home, blog or news page will have the format http://m.website.com/2012/03/the-post-title
When clicked, the htaccess will query permalink.php with the string /2012/03/the-post-title to get the postID, then opens http://m.website.com/article.php?id=postID but the address in the address bar will be http://m.website.com/2012/03/the-post-title and this shows the article in full, be it home page, blog page or news page.
Problem: I have been searching and I'm not exactly sure how to go about (B) above but I know it's possible. In the end, all rules for A and B will be in the same htaccess file in the site root.
Thanks
This should do the trick for you. It looks like you have done most of it so there isn't muc to do.
RewriteEngine On
RewriteBase /
# you do not need a rewrite for the root as you should have index.php
# as your default document and page 1 as your default page
RewriteRule ^page/1/?$ / [NC,L,R=301]
RewriteRule ^page/(\d+)/?$ index.php?page=$1 [NC,L]
# you do not need a rewrite for blog as you should have index.php as your
# default document and page 1 as your default page
# you do not need a rewrite for news as you should have index.php as your
# default document and page 1 as your default page
RewriteRule ^(blog|news)/page/1/?$ $1 [NC,L,R=301]
RewriteRule ^(blog|news)/page/(\d+)/?$ $1/index.php?page=$2 [NC,L]
######################################################################
################## ADD YOUR PERMALINK.PHP CODE HERE ##################
######################################################################
UPDATE
To effectively turn /2012/03/the-post-title into a postID you need to be able to ask your database to do that for you as it is the only thing that knows the answer. So you can either use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html#dbd I have never done this myself and I would advise against it. Not because I know there is a problem with it I just have a bad feeling about it.
The alternative and one I would champion is to just do something like this:-
RewriteRule ^(\d{4})/(\d{2})/([^/]+))/?$ article.php?postIdentifier=$1/$2/$3 [L]
Then in article.php hit the database and use the information in the postIdentifier to get the correct article.
Make sense?

Categories