Sub domain pretty URL in PHP - php

I created a sub domain for my application (http://articles.myloanguroo.com/inside.php/?short=1). Now I want to create a pretty URL for this like (http://articles.myloanguroo.com/inside/1)
My .htaccess file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ inside.php?short=$1
RewriteCond %{HTTP_HOST} !^www\.myloanguroo\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.myloanguroo\.com$ [NC]
This is not working for me and is showing a 404 error.
How can I create subdomain pretty URLs in PHP?

Your regex should start with ^inside/ and keep conditions before RewriteRule:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.myloanguroo\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^inside/(\w+)/?$ inside.php?short=$1 [L,QSA]

Related

Difference with and without www, 301 redirect with codeigniter and htaccess

I have a domain that shows different pages if I leave out the www. Without the www the page freezes, so I want to redirect the http://example.com to http://www.example.com
My question is, how do I change the htaccess so it redirects to the www version?
Here is my .htaccess:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Add these lines after RewriteEngine On:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
But keep in mind, making redirecting rule is just a compromise for your application, not solution for your page freezing problem.
A friend solved my problem by adding the following code underneath the existing code:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Now everything works perfectly! The complete .htaccess file now looks like:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

How to optimize a php page to include users profile page in search results?

I have no idea on how to optimize my profile.php page to include it in search results. i also tried to include meta description but nothing is happening.
I have asked the same question somewhere else, but they said it must be because of your .htaccess file.
here is my .htaccess file :
Options +Indexes
# or #
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9._-]+)$ profile.php?u=$1
RewriteRule ^([a-zA-Z0-9._-]+)/$ profile.php?u=$1
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Actually your rules aren't correct.
In general you should have 301 rules before internal rewrites.
You don't need a separate rule for trailing slash only.
Your 2 rewrite conditions won't apply for 2nd rewrite rule.
Try these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9._-]+)/?$ profile.php?u=$1 [L,QSA]

Using mod-rewrite with multiple pages

Simplified Question
I changed domain.com/folder/client.php?id=1 to 1.domain.com with this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now I want to change domain.com/folder/about.php?id=1 to 1.domain.com/about.
Is it possible ? or is there any other ways to do this?
Thank you :D
Old Question
In my directory, I have 2 pages (client.php, about.php). I've already set a wildcard subdomain to the page directory and changed domain.com/folder/client.php?id=1 to 1.domain.com. This is my .htaccess code :
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
Now, I need to add another page. So, in client.php page, there's a button to go to about.php page in the same directory. I tried 1.domain.com/about.php and it's not working.
Do I have to create another line of RewriteCond ? or is there any other ways to do this?
Thank you very much :D
Updated Code without working id
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [QSA,NC]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . /about.php?id=%1 [L,QSA]
**The code resolved the text in url box. Let's say id=1. Now it's showing 1.domain.com/about.
the php code : `$_GET["id"]` is not specified.**
Thanks to faa for the edit :D
The Full Solution
Refer Using mod-Rewrite with 5 pages (wildcard sub-domain)
You may try this:
Options +FollowSymLinks
RewriteEngine on
## This is the actual rule-set that works as it is according to the OP
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
# Added to exclude folder `/about`
RewriteCond %{REQUEST_URI} !^/about [NC]
RewriteRule ^$ /client.php?id=%2 [L,QSA,NC]
## This is the additional rule set
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/about [NC]
RewriteCond %{REQUEST_URI} !about\.php [NC]
RewriteRule . http://domain.com/folder/about.php?id=%1 [L,QSA]
Maps silently
http://N.domain.com/about with or without trailing slash
To:
http://domain.com/folder/about.php?id=N
For permanent and visible redirection, replace [L,QSA] with [L,R=301,QSA]

URL rewriting with subdomain structure in php

I am trying to create a URL like category.\ArticleName
This is what I have written in my .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).msite.com/([a-zA-Z]+) [NC]
RewriteRule (.*) $1/Html/HomePage.php?Category=%1&name=%2 [NC,QSA]
I actaully tried
RewriteCond %{HTTP_HOST} ^(.*).msite.com$ [NC]
RewriteRule (.*) $1/Html/HomePage.php?Category=%1 [NC,QSA]
It worked fine means wild-card subdomain etc are enabled.
Close, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*).msite.com$ [NC]
RewriteRule (.*) /Html/HomePage.php?Category=%1&name=$1 [L,NC,QSA]
You'll need the two extra conditions in the beginning so that valid requests don't get routed to HomePage.php.

.htaccess Only for www. Subdomain

How do I construct my .htaccess file so that it adds www to the beginning of the address mysite.com, but doesn't add www. to the beginning of other subdomains such as test.mysite.com? My .htaccess file currently looks like this:
Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
Something like:
RewriteCond %{HTTP_HOST} ^mysite.com
Instead of
RewriteCond %{HTTP_HOST} !^www\.
I just don't know the darn rule to get only the characters for domain naming so i'll try this but it might fail on some subjects:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[a-zA-Z0-9\-]+(\.[a-z]{2,4})+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
This will force all domains that START with one word and a TLD to be forwarded to www.samedomain.tld but subdomains wont be hit...

Categories