简体   繁体   中英

.htaccess is not working on all links

I have two URL samples in my new site.

  1. sample1: http://localhost/phone/3
  2. sample2: http://localhost/phone/3/10

please note: phone is the project name

And I wrote an htaccess file myself, but it's only working with sample1.

Options +FollowSymLinks
RewriteEngine On

#RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?menu=$1 [L]

I need a version that works on both samples. Any help would be appreciated.

Use this:

RewriteRule phone/([^/]+)$ /index.php?menu=$1
RewriteRule phone/([^/]+)/([^/]+)$ /index.php?menu=$1&some=$2

Or if your params are always integers:

RewriteRule phone/([0-9]+)$ /index.php?menu=$1
RewriteRule phone/([0-9]+)/([0-9]+)$ /index.php?menu=$1&some=$2

I suggest you use ([^/]+) instead of (.*)

The difference is that ([^/]+) doesn't include forward slashes. (.*) fails when you have a url that ends with a slash. For example:

home/([^/]+)/?$ index.php?menu=$1 

will work perfectly for

sitename.com/home/contact

and

sitename.com/home/contact/

you'll get a $_GET['menu'] containing contact with both url. With (.*) the second one will contain contact/ so with a trailing forward slash.

ending with /?$ means that the trailing / is optional at the end.

Edit:

complete code in htaccess format:

RewriteEngine on 
RewriteRule ^phone/([^/]+)/?$ index.php?menu=$1
RewriteRule ^phone/([^/]+)/([^/]+)/?$ index.php?menu=$1&var2=$2
RewriteRule ^(.*)$ /index.php?menu=$1
RewriteRule ^(.*)/(.*)$ /index.php?menu=$1&some=$2

UPDATED Again

Options +FollowSymLinks
RewriteBase /phone
RewriteEngine On    
RewriteCond %{REQUEST_FILENAME} !-f # Do not rewrite static files
RewriteCond %{REQUEST_FILENAME} !-d # Do not rewrite static directories
RewriteRule ^(.*)$ /index.php?menu=$1
RewriteRule ^(.*)/(.*)$ /index.php?menu=$1&some=$2

Try this

This should do what you want:

Options +FollowSymLinks
RewriteEngine On

RewriteBase /phone/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*?)/?$ index.php?menu=$1 [L]
RewriteRule ^(.*?)/(.*?)/?$ index.php?menu=$1&submenu=$2 [L]

//OR the following if the values are always going to be integers (This is the best way)

Options +FollowSymLinks
RewriteEngine On

RewriteBase /phone/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([0-9]+)/?$ index.php?menu=$1 [L]
RewriteRule ^([0-9]+)/([0-9]+)/?$ index.php?menu=$1&submenu=$2 [L]
RewriteRule ^(.*?)/(.*?\.js)$ /phone/js/$2 [L] #Makes JS static content work
RewriteRule ^(.*?)/(.*?\.css)$ /phone/css/$2 [L] #Makes CSS static content work

I wouldn't recommended using (.*?). Its best to be specific what you want the value to be.

Absolute paths would be better over static paths.

Static Path

<script src="script.js" type="text/javascript"></script>

Absolute Path

<script src="http://localhost/phone/script.js" type="text/javascript"></script>

If your worried and transportability you could create a settings file, with this in:

define("WWW_DOMAIN", "http://localhost/phone/");

Use this on the top of every page as:

And then do:

<script src="<?php echo WWW_DOMAIN;?>script.js" type="text/javascript"></script>

Make a website easier to develop locally before moving it do a different server. Then all you have to do is change WWW_DOMAIN once.

Might be easier than doing it with htaccess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM