Well URL rewriting is nightmare and I am having that for many days.
I am trying to rewrite url like:
example.com/playground.php?room=abc123 TO example.com/room/abc123
example.com/index.php?room=abc123&action=join TO example.com/join/abc123
I am being able to successfully rewrite the first URL (as in the first case) but am not being able to rewrite the second URL properly. It is being rewritten as example.com/room/join/abc123. I understand that the first rule is being applied for both cases and then the execution of the htaccess stops. But then how to manage to get the desired solution?
HTACCESS
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^room/([A-Za-z0-9]+)$ playground.php?room=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^join/([A-Za-z0-9]+)$ index.php?room=$1&action=join [L]
You problem is not in the URL rewriting, but it has to do with your playground.php. It has relative URLs instead of absolute URLs. Please change join/abc123 to /join/abc123.
Also there is a tiny error in your .htaccess
You should change
RewriteCond %{REQUEST_FILENAME}.php !-f
to
RewriteCond %{REQUEST_FILENAME} !-f
Related
So having very little experience with Regex as well as minor beef with .htaccess I need to ask this:
I want to use AJAX on my site, but my mod rewrite configuration prevents me from doing this properly, as I redirected all urls to my index.php and set the url $_GET variable, this is my current setup:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Now I want this rule to not fire if the query contains "&ajax&" as a string, at least I always append "&ajax&time=..." to the ajax path that I want to load.
So I thought it would be easiest to check if I can match "ajax" and use the [S]kip or [L]ast Flag to prevent the other rule to fire, but haven't been able to achieve it yet, any help? With explanations or pointers toward some?
Also, it would be neat if this doesn't require absolute urls or anything, as I want it to work in both development (localhost) and live (actual domain) environment without changing the .htaccess file accordingly.
SO
I want the url example.com/profile
to be redirected to example.com/index.php?url=profile
AND
I want the url example.com/src/file.php?stuff=something&ajax&time=1234
to stay example.com/src/file.php?stuff=something&ajax&time=1234
BUT
I want the url example.com/ajax
to also be redirected to example.com/index.php?url=ajax (just in case)
Any help is greatly appreciated, thanks!
You just need to add another RewriteCond in your rule that skips this rule if query string contains ajax:
Options -Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} !(?:^|&)ajax [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
The htaccess file shouldn't rewrite requests for valid files. This is the line that says so:
RewriteCond %{REQUEST_FILENAME} !-f
So what you want should already be happening, assuming src/file.php exists on the server.
I have php that works awesome
But I wanna change it from showing variables to look like dirs
eg:
example.com/?mode=page&page=15 to example.com/page/15
and
example.com/?mode=pic&picid=136 to example.com/pic/136
however I only know of one way to do it, such as:
RewriteRule ^(.*)$ index.php?mode=$1&page [R]
but that only works for one case, otherwise it 404s
I tried using RewriteRule ^(.*)$ index.php?url=$1 [L,QSA] so that the whole path gets passed. However, this way it doesn't seem to pass the CSS in my pages.
I'm extremely confused...
Here's my htaccess file
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The problem is you are rewriting ALL urls to
index.php?url=
So your CSS files also get "redirected" to "index.php?url=" eg
index.php?url=css/styles.css
So you need to add "conditions" to your .htaccess so that certain files (or directories) dont get "redirected". You can do this by using "RewriteCond" (conditions) before your "RewriteRule ".
For example to not "redirect" the css file "css/styles.css" you could do
RewriteCond %{REQUEST_URI} !^css/styles\.css$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Here is what I am trying to do:
When a file is requested from filesystem and it does not exist, rewrite the URL to /index.php?404
When file is requested and it does exist in filesystem, rewrite the URL to /index.php?file
In every other case rewrite the URL to /index.php?data
But I am getting 500 errors as a result, does anyone know where the problem might be? I have used RewriteEngine in the past, but it's still a bit confusing to me regarding how to use it for special cases like this.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} (.*\.([a-zA-Z]{2,4}))$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php?404 [L]
RewriteCond %{REQUEST_FILENAME} (.*\.([a-zA-Z]{2,4}))$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* ./index.php?file [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php?data [L]
You have infinite rewrite loop. To solve -- add extra condition to not rewrite already rewritten URLs .. or at least ignore requests to index.php.
One of the possible approaches:
# do not touch any requests to index.php
RewriteRule ^index\.php$ - [L]
P.S.
How L flag works: RewriteRule Last [L] flag not working?
Let me preface by saying that I'm fairly new to .htaccess authoring and have usually left the nitty gritty up to my hosting provider. I've recently decided to implement a hand rolled MVC framework in php and was using the .htaccess file to redirect using "seo" friendly urls. My MVC implementation uses the variables module, class, event, and parameter. They are specified in the url in that order, e.g. http://mydomain.com/module/class/event/parameter. I want it to work if you chop off any part of the url. This was all working fine until I moved my site up one level and subsequently copied my .htaccess file. Now I'm getting an infinite redirect loop with one of the rules that was working fine before I moved it.
Here is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^$ /index.php?module=default&class=home [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ \
/index.php?module=$1&class=$2&event=$3 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ \
/index.php?module=$1&class=$2&event=$3¶meter=$4 [QSA,L]
Now, if I take out the section that reads:
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?module=$1 [QSA,L]
It works great. The problem is if I leave that out and someone types http://mydomain.com/module I get a 404 (since that directory doesn't exist and none of the rules are matching). So why does that rule now not work and why did it work when index.php (and this .htaccess file) are in a sub-directory?
Well it looks like the php router solution is the best. Thanks to #prodigitalson for the heads up.
Here is the simplified .htaccess I am now using
RewriteEngine On
# Disable rewriting for existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirect all other requests to index.php
RewriteRule ^.*$ index.php [PT,L]
RewriteRule ^$ index.php [PT,L]
And this "magic" line of code does all the heavy lifting that the .htaccess was doing before:
$url = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
Now I can assign my module, class, event, and parameter values from the $url array. It also solves a problem I had where I may need more than one parameter to an event. I didn't want to have to keep adding rules to the .htaccess file for each level. Now I can supply an arbitrary number of parameters and deal with the routing logic in code.
Sorry I couldn't come up with a better title.
Here is my htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]
And this is my problem:
localhost/validpage gives me the contents of localhost/validpage.php.
localhost/validpage/blah also gives me the same (validpage.php) page and so does localhost/validpage/blah/blah/...
Therefore the problem I am facing is link duplicity(in my words!).
How do I allow localhost/validpage.php to be accessed from localhost/validpage only and nothing else, not even localhost/validpage.php.
I have started a question on *Server****Fault*** too but with not much success.
The answer I have got is it cannot be done with htaccess alone.
By validpage I mean any valid page on the server. Since I am retrofitting an existing site with mod_rewrite for cleaner urls, I am looking for a relatively easy solution preferably with .htaccess only. However, any solutions are welcome.
what is the source attribute of your images, etc ???
absolute or relative?
<img src="/images/my.jpg" /> and <img src="images/my.jpg" /> point to different files when applying your rewrite rules.
You could try using this in your .htaccess
RewriteEngine on
RewriteBase /
# if request has a php extension remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^((.*)\.php)$
RewriteRule ^(.*).php$ $1 [L,R=301]
# if request uri has no extension link to php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I should rewrite your php scripts to friendly urls, and redirect requests using the .php extension.