htaccess regex with two periods in file name - regex

I have this current expression, that takes site.com/index.php to site.com/index
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I need to make it accept filename with two periods in them, like site.com/core.index.php to site.com/core.index
Any help would be appreciated!

The regex ^(.*)(?<!\.php)$ matches every address not ending with ".php", so
RewriteRule ^(.*)(?<!\.php)$ $1.php [NC,L]
could work. I am not that familiar with RewriteRule, though.

More simple solution also available:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f # We don`t have such file (includes !-d)
RewriteRule ^(.*)$ $1.php [NC,L] # Just add .php to the end

Use this regex ^[\w\d]*\.[\w\d]*\/[\w\d]*\.[\w\d]*

Related

htaccess removing .php extension and pretty urls

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]

.htaccess rewriterule with backslashes in regex

I want to 'catch' an URL with .htaccess rewriterule. How can I add backslashes to the rewriterule?
For example:
http://www.example.com/page1 -> index.php?page=page1
http://www.example.com/page1/subpage1 -> index.php?page=page1/subpage1
As you can see, I want the backslash between 'page1' and 'subpage1' IN the rewriterule, because the path can be variable (more subpages).
I tried this:
RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]
But then I get an Internal Server Error.
Thanks in advance!
You get an Internal Server Error because your rule introduces an infinite redirect loop.
Here is what you want
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]
It checks if the url is not a physical folder/file before rewriting it internally

Use .htaccess to replace after the first slash but keep query

I have the following link:
http://example.net/invite/12345
I need to replace it to http://example.net/wedding/index.php?main_page=invite&invite_code=12345
I've got the following....but I'm just not hitting the nail here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /wedding/index.php?main_page=invite&invite_code=$1 [L,QSA]
It's taking me to:
http:///example.net/wedding/index.php?main_page=invite&invite_code=invite%2F12345
Thanks for the help!
$1 is a reference to the capturing group (.*), so:
RewriteRule ^invite/(.*)$ /wedding/index.php?main_page=invite&invite_code=$1 [L,QSA]

Apache URL rewrite rule and regex

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.

URL Regex - remove trailing slash from file name and end of URL?

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