So I have this regex problem and wondered if anyone could help me out?
If a user visits http://example.com/index.php/ how can i modify/add to my regex to prevent a trailing slash(s) at the end?
also
I currently have a page, called post.php that can be accessed like so http://example.com/reviews/reviewTitle/ and http://example.com/news/newsTitle/
again, how could I prevent this trailing slash?
Below is the regex I have so far:
RewriteEngine on
RewriteRule ^reviews/([^/\.]+)/?$ reviews/post.php?title=$1 [L]
RewriteRule ^news/([^/\.]+)/?$ news/post.php?title=$1 [L]
RewriteRule ^page/(.*) index.php?page=$1
Note: Im also re-writing http://example.com/index.php?page=1 to http://example.com/page/1 etc, same question, how can I prevent a trailing slash?
Many thanks, I really appreciate any help :)
Try adding this rule after RewriteEngine on
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301]
RewriteRule ^reviews/([^/\.]+)$ reviews/post.php?title=$1 [L]
RewriteRule ^news/([^/\.]+)$ news/post.php?title=$1 [L]
RewriteRule ^page/(.*) index.php?page=$1
Edited to show full set of rules
Edited 2nd Time Added in
RewriteCond %{REQUEST_FILENAME} !-d
above new rule to allow directorys to still be accessed without causing an infinite redirect loop
Related
Im trying to both remove .php extensions. So for example "http://localhost/timetable/login" instead of "http://localhost/timetable/login.php"
But also have
"http://localhost/timetable/38/" instead of
"http://localhost/timetable/index.php?week=38"
Im able to get one or the other working but not both at the same time. Im assuming its because there is a conflict between them but Im not advanced enough to find it.
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([0-9]+)$ index.php?week=$2
RewriteRule ^([0-9]+)/$ index.php?week=$2
If in the address bar I type "http://localhost/timetable/38" it brings me to "http://localhost/38/" and an Object not Found error.
Does anyone know what the problem is ?
UPDATE: I can now go to the page but
echo $_GET['week'];
Is returning empty result, so its ignroing the 40 in "http://localhost/timetable/40"
Instead of using separate rewrite rule for each input, you should consider routing all of them as a single string to some php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
In you php file, you can then separate the string as input and use them as required.
<?php
$inputs = explode('/', $_GET['page']);
You only have one capture group when you try to get the week. So it should be $1 instead of $2.
According to this test tool, the following should work:
RewriteRule ([0-9]+)/?$ index.php?week=$1
I would do something like this:
# rewrite if url ends with a number and possibly a slash
RewriteRule ([0-9]+)/?$ index.php?week=$1 [QSA,L]
# do not append .php if it already ends with .php, other add .php
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [QSA,L]
My current .htaccess looks like this
RewriteEngine On
RewriteBase /
# / = /home
RewriteRule ^$ index.php?r=home [QSA,L]
# /example/ = /example
RewriteRule ^(.*)/$ /$1 [L,R=301]
# request, but not pointing to an existing file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# pass the whole request as a variable
RewriteRule ^(.+)$ index.php?r=$1 [QSA]
After these rewrites are done, I'd like to make sure that the URL doesn't end up having a duplicated part of a path at the end.
Here are a few test cases to more clearly demonstrate what I'd like to accomplish:
domain.tld/example // No more rewrites
domain.tld/example/example // Rewrite to domain.tld/example
domain.tld/test/example/example // Rewrite to domain.tld/test/example
domain.tld/example/test/test/test // Rewrite to domain.tld/example/test/test
So basically, if the last part of the URL is same as the penultimate, then remove the last part.
I've managed to put together this regex:
\/(.+)\/{1}\1$
which seems to match any duplicated parts of the URL, but I don't know if this is quite right for my needs, or how to implement the actual URL rewrite itself.
Any help with this would be greatly appreciated.
Figured out that I can use the following RewriteRule to accomplish this.
RewriteRule (.+)\/\1$ $1 [L,R=301]
probably my question duplicate with an other title but, I have read too many title and checked many times with google. So if it duplicates, I am sorry.
Now I have an URL:
www.mysite.com/profile.php
I am using this URL rewrite rule:
RewriteRule ^([A-Za-z0-9-_]+)/?$ profile.php?username=$1 [NC,L]
And I can change my url like:
www.mysite.com/username
Then, I need to update my url with an other get parameter:
www.mysite.com/username/photos
At this part, I have an URL like:
www.mysite.com/profile.php?username=xxx&w=photo
I have tried this URL rule for the url for above:
RewriteRule ^([A-Za-z0-9-_]+)/?$/?$ profile.php?username=$1&w=$2 [NC,L]
But It does not work. Please help.
Thank you very much.
-------UPDATE------
Now I can use profile.php which It should be. But other rewrite rules are broken. My current .htaccess file like this:
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
RewriteRule ^p/timeline timeline.php [NC,L]
RewriteRule ^p/notifications notifications.php [NC,L]
First thing first:
[A-Za-z0-9-_]
is not a correct regex because of unescaped hyphen between 9 and _ in a character class that acts as a range between hex 39 and hex 5f. To fix this make sure to use hyphen at first or last position in a character class.
Correct rules will be:
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
Update:
As per your updated question make sure to use generic rule after specific rule. So have your rules like this:
RewriteRule ^p/(notifications|timeline)/?$ $1.php [NC,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?username=$1&w=$2 [QSA,L]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [QSA,L]
RewriteRule ^([A-Za-z0-9-_]+)/(.+)$ profile.php?username=$1&w=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-_]+)$ profile.php?username=$1 [NC,L]
You can just have one rule if you want. I would also add the conditions so that it makes sure it's not a real directory or file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/?([A-Za-z0-9-_]+)?/?$ /profile.php?username=$1&w=$2 [NC,L]
In your PHP you will just have to check the w parameter to see if it's empty or not. If it's not, display the details also.
I am looking to find a way to match a URL like www.domain.tld/about or www.domain.tld/contact within my .htaccess file. The rule has to be dynamic as the pages come from a CMS so the rule needs to be able to accept any newly created page.
Currently I have the following rule:
RewriteRule ([^/]+)$ ?cat=generic&page=$1 [L]
The issue is that without a trailing / or anything else to help identify the catch, it just triggers a 404 error page. I used to have the rewrite as:
RewriteRule ([^/]+)/$ ?cat=generic&page=$1 [L]
but decided to not have trailing slashes on the end of URL's, unless its a folder path.
Thank you anyone who can help on the issue.
Put this rule on top of all other rules i your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^broadleaf$ /products/desktops/broadleaf-one [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /?cat=generic&page=$1 [L,QSA]
/?$ makes trailing slash optional.
I'm trying to redirect old-style links from an old website to new style links in php.
I'm using:
RewriteEngine on
RewriteBase /
RewriteRule s\.cfm?id=5$ http://mysite.com/article5.php [B,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
However, if I use just s.cfm? everything works fine and redirects to article5.php. But if I try to redirect the id=5 part, I get page not found.
I tried just s.cfm?id and it causes the htaccess bug. So anything you put after question mark ?... causes a problem, I don't know why.
You can't match against the query string inside a RewriteRule, only the URI path is sent through the rule itself. If you need to match against the query string, then use the %{QUERY_STRING} var inside a RewriteCond:
RewriteCond %{QUERY_STRING} ^id=5$
RewriteRule ^/?s\.cfm$ http://mysite.com/article5.php [L,R=301]
Everything else is fine.