Rewrite rule not working as expected?

2019-09-06 20:13发布

I have a URL with a parameter which I wish to make into sef URL:

want:

http://map.tautktiv.com/street.php?address=abc

to become:

http://map.tautktiv.com/street/address/abc

or

http://map.tautktiv.com/address/abc

have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)

these are the rules I tried:

1.

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1

2.

Options +FollowSymLinks
RewriteEngine on

RewriteRule /address/(.*)\.php street.php?address=$1

3.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here

RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1

the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.

tried also this per below suggestions

Options +FollowSymLinks
RewriteEngine on 

RewriteBase /

RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]

same result, nothing happens.

tried to go directly to page from main domain but same result:

http://tautktiv.com/map/streets/street.php?address=abc

标签: .htaccess
2条回答
劫难
2楼-- · 2019-09-06 20:56

Try this one:

RewriteEngine on 

RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]

In your examples you'd missed ^ and $ in the second row of RewriteRule.

And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-06 21:03

First rule will redirect your ugly URL to the pretty URL. Second rule will internally redirect it back so the user will not see the ugly URL.

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]

# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]

If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.

The .htaccess should go inside the folder where street.php is located.

HTTP is US ASCII so your language would fail, it will redirect it to something like this:

/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C

Your best bet here would be to change the links to use /street/address/word instead of the php file directly.

This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.

查看更多
登录 后发表回答