Rewriting URL with .htaccess local in XAMPP

别来无恙 提交于 2019-12-17 20:36:33

问题


My .htacces begins with

   RewriteEngine on
   RewriteBase /

(I tried it also without RewriteBase...)

I tried all of the following rewriting rules to rewrite the URL

index.php?page=news

to

/blog

  • RewriteRule ^/?([-A-Za-z0-9]+)/([-A-Za-z0-9]+)/blog$ index.php?page=$1 [L]
  • RewriteRule ^([^/]*)/blog$ /sites/blog/index.php?page=$1 [L]
  • RewriteRule ([a-zA-z]+)/([a-zA-z]+)/blog$ index.php?page=$1 [L]

Nothing works - no error. Mod_rewrite is installed and working. I restarted Apache and MySQL everytime I changed something in my .htaccess.

I also want to change my URLs which looks like this... index.php?page=single_news&category=release&id=9&headline=Beastie%20Boys%20III

...into: blog/release/9-Beastie-Boys-III

I am lost. Hope you can help me.


回答1:


First of all, upload your .htaccess and other files (whole project) to some working, ready hosting server. And check, if your rewriting works OK there. This will let you know, if this is problem with .htaccess or XAMPP itself. I had many strange problems with using .htaccess locally, under XAMPP, that were magically gone, after files were uploaded to Internet hosting.

For example, I don't have working autorization using .htaccess locally, because right after I provide correct login and password I see exactly the same error message as you mentioned. As for me, I'm more than sure that this problem is purely related to incorrect interpretation of .htaccess done by XAMPP (as everything works like a charm on production server), not by some mistakes in .htaccess contents.

I wasted (too) many hours on finding solution and left it. For right now, if I'm developing locally, I rename ".htaccess" to "htaccess", so it is ignored by XAMPP (Apache on-board of it) and re-enable it only when deploing files to production server. This approach maybe isn't to professional, but it saved me a lot of time and stress! :]

On the other hand, if your hosting also fail with the same symptoms, then you'll know, that this is not XAMPP releated problem and you have something wrong with your syntax.

Take a look here for a similar problem reported on StackOverflow.com, where (as I think) the cause is the same as in your issue.




回答2:


Here's the solution to change links from http://www.domain.tld/index.php?page=blog to http://www.domain.tld/blog is:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+$ index.php?page=$0 [L]
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{QUERY_STRING} ^page=(\w+)$
RewriteRule ^index\.php$ /%1? [R=301,L]

and for links like: http://www.domain.tld/index.php?page=single_news&id=1&headline=This%20Is%20A%Headline

the solution is:

RewriteRule ^blog/(\d+)-([\w-]+)$ index.php?page=single_news&id=$1&headline=$2

After using this code, links looks like this: http://www.domain.tld/blog/2-this-is-a-headline




回答3:


For your first question, try this:

RewriteEngine on
RewriteRule ^/blog$ /index.php?page=news


来源:https://stackoverflow.com/questions/11243975/rewriting-url-with-htaccess-local-in-xampp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!