I need a rewrite rule that allows full stops. How can I allow for this? What I want is for
shop/domain/www.test.com to become shop/domain/controller.php?param1=www.test.com
The original rule I have is below:
RewriteRule ^shop/domain/([^/.]+)/?$ shop/stock/controller.php?param1=$1 [NC]
This works but only if I remove the full stop.
I have also tried the following:
RewriteRule ^shop/domain/([^/]+)/?$ shop/domain/controller.php?param1=$1 [NC]
This allows the full stop but then has controller.php is the parameter when it should be www.test.com.
If all domains begin with "www.", you could do this
RewriteRule ^shop/domain/www\.([^/]+)/?$ shop/domain/controller.php?param1=$1 [NC,L]
The problem with your rule is, that it also matches controller.php as if it were a domain. You can add the following two rewrite conditions to prevent this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^shop/domain/([^/]+)/?$ shop/domain/controller.php?param1=$1 [NC,L]
!-d means not an existing directory, !-f means not an exisiting file (e.g. controller.php).
Related
I'm building a simple API with apache and I want to map /really/long/path/api/captions.php?caption_id=blah to /really/long/path/api/captions/blah. It's important to NOT have to specify a full path in the rewrite rule because I want this to work no matter where I deploy this code to. However, I can't find/figure out a working example of .htaccess rewrite rules that enable me to match based upon only the final part of the extension.
So, assuming that I have, say, captions.php in a dir called api, what .htaccess file do I need to include in api to accomplish this transform without having /really/long/path/ anywhere therein?
(I also want to be able to map /really/long/path/api/captions.php to /really/long/path/api/captions/ and /really/long/path/api/captions.)
I've tried all sorts of wildcard-like syntax; here's one of those non-working attempts:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?captions/(.*?)/?$ /captions.php?caption_id=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /captions\.php\?caption_id=([^\&\ ]+)
RewriteRule ^(.*?)/?captions\.php$ /captions/%1? [L,R=301]
Thanks!
Got there in the end. This is all that was needed:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^captions/?$ captions.php [NC,L]
RewriteRule ^captions/(.*)/?$ captions.php?caption_id=$1 [NC,L]
I'm having some problems rewriting URLs with the following rules
RewriteEngine on
RewriteRule ^page/(.*)$ index.php?pag=cms&title=$1 [NC]
RewriteRule ^admin/(.*)$ admin/$1 [NC]
RewriteRule ^(.*)$ index.php?pag=$1 [NC,L]
What I'm trying to achieve is to check if the URL is a cms page or not and leave admin URLs as they are.
If I remove the last condition it works but I will have no rule for not cms pages.
Ideally I would want to have just one rule for every page (cms or not) but I can't figure out how to check that other than using page/ in the URL.
Mod_rewrite will keep looping through all the rules until the URI stops changing (or it reaches its internal redirect limit, resuling in a 500 error). You need to add a few conditions to the last rule so that it won't rewrite URI's that's already been properly routed:
RewriteRule ^page/(.*)$ index.php?pag=cms&title=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=$1 [NC,L]
Additionally, the second rule does nothing except a passthrough, so you can replace it with
RewriteRule ^admin/(.*)$ - [NC,L]
You need these rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
From the Apache documentation:
'-d' (is directory)
Treats the TestString as a pathname and tests whether or not it exists, and is a directory.
'-f' (is regular file)
Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
I have an .htaccess file that rewrites urls for SEO purposes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [L,NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Now the first rewrite rule works fine. (www.domain.com/user goes to display.php?page=user)
but the second one should work like (www.domain.com/search/something/else must go to search.php?what=something&where=else
What am I doing wrong here?
Your second rule is incorrect for what you are looking for. You are requesting the result of two captures, but are only making one capture.
Try this instead:
RewriteRule ^search/([^/\.]+)/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
Edit: You'll also need to switch your rules around. Your first rule captures everything, and would therefore discard the second.
So, swap them around, and use the L flag, as suggested already.
You are using the [L] flag, which causes mod_rewrite to stop processing the rule set.
Try removing the L flag from the first Rewrite Rule, like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /display.php?page=$1 [NC,QSA]
RewriteRule ^search/([^/\.]+)/?$ search.php?what=$1&where=$2 [L,NC,QSA]
This question might be a duplicate. But I did not find any solution worked for me.
I want to rewrite URL, where I have one and two level parameters. first parameter is p and second is sp
www.domain.com/home should point to www.domain.com/index.php?p=home
and
www.domain.com/projects/99 should point to www.domain.com/index.php?p=projects&sp=99
How do I do in .htaccess?
Currently My htaccess is as followes,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1
RewriteRule ^([^/]*)/([^/]*)\$ index.php?p=$1&sp=$2 [L]
The problem with this htaccess is that it correctly points one level url. ie., www.domain.com/home. But not the two level url. ie. www.domain.com/projects/99
You have to treat the rules separately. All Conditions preceding rules only apply to a single, immediately following rule. You tried to 'chain' two rules. The second rule never could have matched, since the first one was a catch-all that changed the syntax. Apart from that you have to make sure that the first rule does not catch unwanted requests. Also think about whether you want to use the * or the + operator in the patterns. I suggest you use the + operator, so that you have a clear error message when empty values are requested for a 'page' or a 'subpage'.
So this might come closer to what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?p=$1&sp=$2 [L]
I am working on a site that uses pages with _get variables for example you can get to www.thewebsite.com/users.php?uservar=username by just using www.thewebsite.com/users/username.
The issue I run into is when I also try to add in url rewrites that also cut off file extenuation so aboutus.php becomes about us.
Can I have these two functions in one .htaccess file?
I tried this but it did not seem to work well
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /user.php?page=$1 [L]
You can have both at the same time, but you need the routing rule (the one that targets user.php) to be below the rule that tries to re-attach the extension. So something like this:
# whatever rule you have to re-attach the extension:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
# now your user routing rule, make sure you add the condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /user.php?page=$1 [L]