I am trying to make a user system, where the URL is like:
test/user/[name]
It all works, but if you try:
test/user/name/something
It doesn't work. Is there any way to remove the slashes from that certain point, so that:
test/user/name/something
will go to:
test/user/name
How do I edit this, so it works as mentioned above:
RewriteEngine on
RewriteCond %{REQUEST_URI} user/(.*)
RewriteRule user/(.*) something/test/user.php?user=$1
You can place this rule as your first rule in your root /test/.htaccess:
RewriteEngine on
RewriteBase /test/
RewriteRule ^(user/\w+)/.+$ /$1 [L,NC,R]
RewriteRule ^user/(.+)$ user.php?user=$1 [L,QSA]
Related
I would like to redirect mysite.com/index.php to mysite.com/index.php?id_category=12&controller=category
Here is my htaccess file.
But it doesn't work. Chrome says : This webpage has a redirect loop.
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule index\.php http://mysite.com/index.php?id_category=12&controller=category
This should work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://www.mywebsite.com/index.php?foo=bar [R=301,L]
The first condition checks if the URI is equal to index.php and the second one checks if GET values are empty. The AND between the 2 conditions is implicit here.
Probably you should change use another file name instead of index.php for the destiny. But you can try:
RewriteRule index.php index.php?id_category=12&controller=category
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]
Ok I have the following .htaccess and it works however I can seem to change it to the way I need it.
here is the current .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
Options +FollowSymlinks
I need to add the following
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&tpl=$2 [L]
So i am wondering how do I get that rule to work? as when I add it it does not.
So if you want to add a rule that will handle all requests except the first one that matches the RewriteCond And those would be split into /?
I think something like this would work. Please note that I changed the $1 to ${REQUEST_URI} in your rewrite condition
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
RewriteRule ^([^/]*)/?(.*)?$ /index.php?cat=$1&tpl=$2 [L]
Options +FollowSymlinks
I'd like to map:
mywebsite.com/users/ -> mywebsite.com/users/users.php
mywebsite.com/users -> mywebsite.com/users/users.php
mywebsite.com/users/username -> mywebsite.com/users/user.php?name=username
At present, I'm using this .htaccess in the users directory:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_])*$ user.php?name=$1 [L]
RewriteRule ^.*$ users.php [L]
However, it never generates the user.php?name=$1 URL.
Why doesn't it work?
Here are the rules (place in .htaccess in website root folder. If placed elsewhere some tweaking is required):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# 1
RewriteRule ^users/?$ /users/users.php [L]
# 2
RewriteCond %{REQUEST_URI} !^/users/users?\.php
RewriteRule ^users/([^/]+)$ /users/user.php?name=$1 [QSA,L]
Rule #1 will match fist 2 URLs of yours.
Rule #2 will work with specific user mapping. It will ensure that it does not rewrite already rewritten URLs.
UPDATE:
If you want to place it into .htaccess file in /users/ folder, then this URL mywebsite.com/users (without trailing slash) most likely will not work.
But in any case -- here are the rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# 1
RewriteRule ^$ users.php [L]
# 2
RewriteCond %{REQUEST_URI} !^/users/users?\.php
RewriteRule ^([^/]+)$ user.php?name=$1 [QSA,L]
It looks to me like it is applying both rules in sequence: the url matches the first rule, so it adds user.php?name=$1, and then it matches the second rule, so it replaces the string with users.php. If that is the problem, then for your particular case you could fix it by replacing the second regular expression with ^/?$.
The line with rewriting index.php and the other three lines work perfectly, but not when put together, I've tried all possibilities with position of the lines, and to be honest pretty exhausted.
My code below:
RewriteEngine On
RewriteRule (.*) index.php
RewriteCond %{REQUEST_URI} !^/(admin|admin/.*)$
RewriteCond %{REQUEST_URI} !^/(l|l/.*)$
RewriteCond %{REQUEST_URI} !^/(includes|includes/.*)$
Can anyone help me in this quick question?
The script runs in: http://domain.com/folder/
The .htaccess is also placed in that folder.
The problem is that it rewrites also files in the admin,l and includes folder
I think what is happening here is the first RewriteRule (.*) index.php is set to rewrite everything regardless of if they match the other regex for the 3 RewriteCond's I think maybe moving it to the bottom and reformulating the rule to be more specific to your needs.
There are few problems in your Rewrite code:
RewriteCond should come before RewriteRule
Multiple RewriteCond can be combined into 1
You must use L flag to mark last rule
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(admin|l|includes)(/.*|)$ [NC]
RewriteRule ^ index.php [L]