I have been trying to include dot in my url but .htaccess RewriteRule is not helping me to do so.
I have been using the following expression
RewriteRule ^user/(\w+)/([-a-zA-Z0-9_\.]) index.php?id=$1&route=$2 [L,QSA]
while this allows me to add a dot at the end of url it doesnt allow me to do so in between the url.
for eg if i use
http://localhost/opencart/user/vendor_name.
it doesnot give an error but if i use
http://localhost/opencart/user/vendor.name
it gives an error of page not found.
Can anyone give me an expression that will allow me to do so.
Because your regex is wrong. [-a-zA-Z0-9_\.] will match only single dot after slash.
Try this rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/[-\w.]+)/([-\w.]+)/?$ index.php?id=$1&route=$2 [L,QSA]
Related
OK so I've found every rendition of this but not this specifically. So my urls are in this format
www.inspection.com/users/?action=register
I want to convert it to
www.inspection.com/users.php?action=register
Here's what I've got so far in my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9]+/\?action=[A-Za-z0-9]+$ $1.php?action=$2 [L,QSA]
But error logs say RewriteRule: cannot compile regular expression
Main Goal: After the domain name is the reference to the file I want to call. So append php on the end of that string but keep the query parameters
With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Also make sure that your .htaccess rules file and .php file are residing on same folder.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/?]*)/?\?action=(.+)$ $1.php?action=$2 [NC,L,QSA]
One more thing, you have not created a capturing group on left side of RewriteRule hence you can't use them on the right side of it.
i'm trying to rewrite my url's, but i'm also using a router.
so i want the router to control error pages such as error 404 not found, and not my htaccess file. everything is working perfectly fine, except for the filter string in my htaccess file. this is the rewrite line:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-z\-\_]+)$ index.php?urlpath=$1
so then issue is that it doesn't accept characters like . and /
so this works: https://www.mystem.tk/aurlthatdoesntexist
but this won't work: https://www.mystem.tk/apagethatdoesntexist.php
and this won't work either: http://www.mystem.tk/a/url/that/doesnt/exist
last one: https://www.mystem.tk/a/page/that/doesnt/exist.php
i hope my problem is clear on this way... you can check it out yourself at https://www.mystem.tk/
This is due to your regex [a-zA-Z0-9-z\-\_]+ which doesn't allow dot or forward slashes.
You can use this rule instead of your existing rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?urlpath=$0 [L,QSA]
I am having trouble finding a solution for what I want to do. I am going to have a couple thounsand affiliates signing up on our site. They will provide a username, and basically give out the url www.domain.com/affiliate/username to their clients.
So, the url I will be starting with is www.domain.com/affiliate/home.html?userid=bob (if bob is the username they give).
I need that to check with the mysql database if that userid exists, then redirect it to the www.domain.com/affiliate/bob url.
Right now, I have this in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)$ affiliate/home.html?affiliate=$1 [L]
It obviously doesnt work and Ive never dealt with .htaccess before. Any help is greatly appreciated. Thank you.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/([^/]+)/?$ /affiliate/home.html?affiliate=$1 [L]
As for redirecting to the nicer looking URL if someone enters the one with the query string:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /affiliate/home.html\?affiliate=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /affiliate/%2?%3 [L,R=301]
There's no way to check if a username is in your database before the rewrite happens, using an htaccess file. There's ways to talk to a database in apache 2.4 or using a RewriteMap (which can only be defined in server or vhost config, not in htaccess) but you're better off doing the check in your home.html and just return a 404 if the user isn't found.
See my answer on an other similar question.
You want www.domain.com/affiliate/home.html?userid=bob to be redirected to /affliate/bob. You want /affliate/bob to be rewritten to /affiliate/home.html?userid=bob.
Add this to your .htaccess in your /www/ or /public_html/.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^affiliate=([^&]*)$
RewriteRule ^affiliate/home\.html /affiliate/%1? [R,L]
RewriteRule ^affiliate/(.*)$ affiliate/home.html?affiliate=$1&r=0 [L]
The first rule matches if the user tries to access affiliate/home.html?userid=bob. The RewriteCondition matches the part of the query string (after the ?) that you need to write the fancy url. %1 matches the first 'capturing group' in a rewrite condition. The ? at the end will clear the query string. [R] means it is a redirect (see the linked answer what that does). [L] means it will stop matching if this one matches.
The second rule is an internal rewrite. $1 matches the first 'capturing group' in the RewriteRule. The trailing &r=0 is there to stop the first rule from matching. It requires a RewriteCond %{REQUEST_FILENAME} !-f to function properly, because the fancy url is in the same directory is the file that handles it.
I am trying to redirect /en/news/12345 to http://www.xyz.com/en/newsletters/12345
the only thing is that "en" and "12345" can change but "news" is always the same.
I have the following so far:
RewriteRule /^(.)/news/(.)$ http://www.xyz.com/$1/news/$2 [R=301,L]
but if i go to mydomain/wp-admin then i get an endless redirect?? Any ideas why this is happening?
Thanks
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^en/news/([0-9]) http://www.xyz.com/en/newsletters/$1 [NC]
Please try this, hope it will solve your problem
I think I found the problem in your .htaccess file. Initially you had two conditions that prevented RewriteRule . /swissfil3/index.php [L] (catch-all) from redirecting when the path pointed to an existing file or folder (ignore-on-file):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond only applies to the rule directly followed by the condition.
When you added a new rule directly below these directives, the catch-all rule was not prevented from redirecting existing files or directories like /wp-admin. I.e. every request not matching any rules before catch-all would be redirected to /swissfin/3/index.php.
In additoin you used (.) to capture each part of the path. This would match one arbitrary character, but no more, so your redirect would be limited to /X/news/Y, instead of /XX/news/YYYY..., to fix this you can use (.+) which will match zero or more chars, or even better ([^/]*) which will match zero or more characters not equal to /.
You should be able to avoid these problems by using the following code (where the catch-all directive has been moved directly below ignore-on-file, and the rewrite rule matches zero or more chars not equal to /):
# ...
RewriteRule ^([^/]*)/news/([^/]*)$ http://www.xyz.com/$1/news/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /swissfin3/index.php [L]
Here you match every URL that contains a char
I’m trying to use the following .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^images/
RewriteRule (.*) view.php?picid=$1 [L]
RewriteRule ^/user/(.*)$ /users.php?user=$1
I want two things to happen: Whenever someone requests /1234, it redirects to /view.php?picid=1234, and also when someone visits /users/bob, it redirects to /users.php?user=bob.
My code however, doesn’t seem to be working correctly.
There are several ways to do that. Here’s one that should work:
RewriteRule ^user/(.+)$ users.php?user=$1 [L]
RewriteRule ^([0-9]+)$ view.php?picid=$1 [L]
The first rule will catch any request that’s URI path begins with /user/ followed by one or more arbitrary characters. And the second will catch any request that’s URI path begins with / followed by one or more digits.
The initial problem with your rules is that the RewriteRule with (.*) will match everything.
If you do not want it to match a URL with a slash in it (such as users/bob), try ^([^/]*)$
Secondly, after a URL is rewritten, the new URL goes through your rules again. If you want to avoid matching something that has already been rewritten once, you should add a condition like
RewriteCond %{REQUEST_URI} !\.php