2

I needed to redirect example.com/var1/var2/var3/... to

example.com/index.php?myvars=var1/var2/var3/...

This is the code I wrote myself:

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?myvars=$1 [L,QSA]

I really want to understand why this does not work, and what solution would work. I need help from you guys, not just to make it work, but to help me understand. When I try my code, it keeps running forever and the page does not load. I don't know why, because I did not copy this code, I studied regex and htaccess to learn, but it did not work.

1 Answer 1

1

I've taken this form a similar project I had last year, and adopted it a bit. That worked fine for me.

RewriteEngine On
#Optional if you have issues with a subdirectory
#RewriteBase /path/on/your/server/

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

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

The problem in your case is probably that = behind the ! The RewriteCondin my example simply make sure that they do not redirect if it's a valid file or directory. Probably even better than just ignoring the index.php as in your example. If you definately want to do so try: RewriteCond %{REQUEST_URI} !^/index.php that should do the trick as well (untested).

6
  • That worked, but the .css file didn't load. When I tried to load it with the full url, it worked. Why i can't just load things with the name? (it was working before...). os: i'm working on a subdirectory. But the rewriting worked. Jun 28, 2014 at 7:08
  • 1
    Ah, you basically imitate folders, so if you call yourserver.com/ and you have entered a relative path to your css, it looks in your root for your css. If you call yourserver.com/your/subsite/ it looks inside that virtual subfolder for your css, which of course dos not exist. So the easiest way, is to add an absolute path, best define that as a variable or constant, that way you can easily alter it if you move it to a live server for example. Jun 28, 2014 at 7:11
  • Thanks, man. Also, I feel like my entire website depends on a single .htaccess file. Are there any security risks using this? What do you recommend me? Jun 28, 2014 at 7:25
  • It's true, you absolutely rely on that file, but it's the same issue with all CMSs. .htaccess files can't be called via browser as long as your webserver is configured proberly. It's definitely frightening how much power that small file has, but no security issue. PS: If my answer helped you, it would be great if you mark it as accepted. Jun 28, 2014 at 7:30
  • Yep, of course you helped me a lot. Wish you the best! Thank you so much! Jun 28, 2014 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.