Lets think that I have such code in htaccess:
RewriteRule ^admin/registration/?$ admin/qeyd.php [NC,L]
RewriteRule ^admin/login/?$ admin/login.php [NC,L]
RewriteRule ^admin/profile/?$ admin/profile.php [NC,L]
But I want to define a variable for "admin" and write it like this:
admin_directory_variable = custom_name
RewriteRule ^custom_name/registration/?$ admin/qeyd.php [NC,L]
RewriteRule ^custom_name/login/?$ admin/login.php [NC,L]
RewriteRule ^custom_name/profile/?$ admin/profile.php [NC,L]
How can I do it ?
Create admin/.htaccess file and use these rules:
RewriteEngine On
RewriteBase /admin/
RewriteRule ^registration/?$ qeyd.php [NC,L]
RewriteRule ^(login|profile)/?$ $1.php [NC,L]
Apache 2.4 has the Define keyword which you use like this:
Define name value
and use like this:
DocumentRoot ${name}
Unfortunately, it can't be used in .htaccess files. See Apache 2.4 documentation
Related
i've got .htaccess file:
RewriteEngine on
RewriteRule ^category?$ category.php
RewriteRule ^category/([0-9a-zA-Z]+) category.php?name=$1
Need to even use letters "ě,š,č,ř,ž,ý,á,í,é" etc..
How to do that?
You can use:
RewriteEngine on
RewriteRule ^category?$ category.php [NC,L]
RewriteRule ^category/([^/]+)$ category.php?name=$1 [NC,L]
Which accepts all characters
I have similar URLs which I want to redirect but the issue is that the first URL overrides the others. For example
RewriteRule ^user/([a-zA-Z0-9-_]+) user.php?user=$1 [NC,L]
RewriteRule ^user/([a-zA-Z0-9-_]+)/about about_user.php?user=$1 [NC,L]
RewriteRule ^user/message/([a-zA-Z0-9-_]+) message_user.php?user=$1 [NC,L]
But the first RewriteRule overrides the others so if I have something like this
example.com/user/message/myName
example.com/user/myName/about
They are all redirected to user.php. Please am new to this, how can I get it fixed?
I believe you should have a $ sign after your regular expressions, so it becomes:
RewriteRule ^user/([a-zA-Z0-9-_]+)$ user.php?user=$1 [NC,L]
RewriteRule ^user/([a-zA-Z0-9-_]+)/about$ about_user.php?user=$1 [NC,L]
RewriteRule ^user/message/([a-zA-Z0-9-_]+)$ message_user.php?user=$1 [NC,L]
As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php
First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps
Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]
mydomain.com/MyFolder/parameter-1
I have this htaccess RewriteRule -
RewriteRule ^([a-z0-9\-]+)/?$ index.php?c=$1 [NC,L]
The .htaccess file is inside MyFolder and this only accept a single parameter.
How can do a RewriteRule to accept 2 parameters
mydomain.com/MyFolder/parameter-1/parameter-2
Thanks
use this:
RewriteEngine On
# mydomain.com/MyFolder/parameter-1
RewriteRule ^([a-z0-9\-]+)/?$ index.php?c=$1 [NC,L]
# mydomain.com/MyFolder/parameter-1/parameter-2
RewriteRule ^([a-z0-9\-]+)/([a-z0-9\-]+)/?$ index.php?c=$1&d=$2 [NC,L]
I have a cakephp installation in the root of my domain. Now it turns out I need to put another app in there that will reside in a subdirectory. How do I disable the controller/model redirection in cake for just this directory?
The current .htaccess in the root folder looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I've tried modifying it like this, but to no avail:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bildbank$ /bildbank/ [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I am aware that this is a bit of a hack, but there's no way I can get the second app to play nice with cake.
Deizel's answer didn't work for me, but placing the following RewriteRule above the two cakephp rules did:
RewriteRule ^bildbank - [L]
The following rule rewrites www.example.com and www.example.com/ to www.example.com/app/webroot/
RewriteRule ^$ app/webroot/ [L]
This rule rewrites www.example.com/* to www.example.com/app/webroot/*
RewriteRule (.*) app/webroot/$1 [L]
I would throw out your rule and update the wildcard regex in the last rule, (.*), so that it matches any string which doesn't start with bildbank. Something like this might do the trick:
RewriteRule ((?!bildbank).*) app/webroot/$1 [L]
This converts the following strings:
cake
cake.htm
cake/test
bilder
bilder.htm
bilder/test
bildbank
bildbank.htm
bildbank/test
.. into:
app/webroot/cake
app/webroot/cake.htm
app/webroot/cake/test
app/webroot/bilder
app/webroot/bilder.htm
app/webroot/bilder/test
bildbank
bildbank.htm
bildbank/test
.. therefore, excluding your bildbank subdirectory.