Using mod-Rewrite with 5 pages (wildcard sub-domai

2019-07-02 02:36发布

问题:

Let's say I got 5 pages (index.php, about.php, portfolio.php, gallery.php, contact.php).

Let's say the id is 1.

The original url is :

www.domain.com/folder/index.php?id=1, www.domain.com/folder/about.php?id=1 and so on...

Now, I want to change it to :

1.domain.com/index, 1.domain.com/about, 1.domain.com/portfolio, 1.domain.com/gallery, 1.domain.com/contact

Is it possible with mod-rewrite ? And if I'm in the index.php page, whats the <a href="" > that goes to about.php if the mod-rewrite is possible?

Thank you very much :D I appreciate your help :D

EDIT

One more thing, My wildcard subdomain folder is public_html/folder/

The Solution By Anubhava

Edited with subdomain codes :

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# EDIT BY ANUBHAVA: to make http://1.domain.com/ load /about as default page
# Replace /about with any other page you want as default page
RewriteCond %{HTTP_HOST} ^1\.(domain\.com$ [NC]
RewriteRule ^$ /about [L]

RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).domain.com$ [NC]
RewriteRule ^(index|about|portfolio|gallery|contact)/?$ /$1.php?id=%2 [L,NC,QSA]

Make sure your wildcard subdomain directory is where the pages are. Also put your .htaccess file in the directory where the pages are.

回答1:

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 /

# EDIT: to make http://1.domain.com/ load /about
RewriteCond %{HTTP_HOST} ^1\.(domain\.com$ [NC]
RewriteRule ^$ /about [L]

RewriteCond %{HTTP_HOST} ^1\.(domain\.com$ [NC]
RewriteRule ^(index|about|portfolio|gallery|contact)/?$ http://www.%1/folder/$1.php?id=1 [R=302,L,NC,QSA]

Once that is in place create href links like this:

<a href="http://1.domain.com/about">About Us</a> or <a href="http://1.domain.com/contact">Contact Us</a>



回答2:

You need to change in the .htaccess file. write the below code in that it will hide the extension of the file.

   Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^([a-zA-Z])\$ $1.php [NC]

And after that you will give the URL in anchor link like /index and so on.

Cheers.