.htaccess rule appending index.php instead of URI - php

My .htaccess looks like this..
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/members/invite/$1 [R,L]
</IfModule>
I want to be able to redirect an URL that looks like this..
https://sub.domain.com/ZTHF76
to...
https://www.domain.com/members/invite/ZTHF76
But instead I am getting
https://www.domain.com/members/invite/index.php
I need to keep the first rule to remove index.php from the URI of the rest of the application.
Any help with this greatly appreciated.

You should reorder your rules and keep redirect rule before rewrite one:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/members/invite/$1 [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Related

Rewrite URL dont get values and error 404

My Htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ %2-%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+?)-([0-9]+)$ index.php?id=$2&title=$1 [L]
</IfModule>
I have in php urls as this :
index.php?id=34234&title=Hello_World&name=Mike
In the link i put this :
TEST
And i try get this url
http://www.test.com/34234/hello_world/mike
But all time give error as 404 error and i don´t know which it´s the solution for this, Thank´s in advanced
Your original URL has 3 path segments while your rule accepts only two. You need to fix your regex patterns
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?id=([0-9]+)&title=([^\s&]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^/]+)/(.+)/?$ index.php.php?id=$1&title=$2&name=$3 [L]
</IfModule>

Get URI params without index.php

I have a URL like http://example.com/index.php/fruit/1a2b3c
I want to get the URI's for which I have written a code.
Now I want to remove the index.php from the visible URL, it should work even when the URL is http://example.com/fruit/1a2b3c and it should still point to index.php
I am on apache2 using PHP 7
Add following code to your .htaccess file.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
change your .htaccess file to,
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
hope this will work.

htaccess make dynamic url to static url

I have a URL like:
http://localhost/admin-s/index.php?m=user
However, I would like a URL like this:
http://localhost/admin-s/user
I try like this:
RewriteRule ^([a-z0-9]+).php$ index.php?nol&m=$1 [L]
RewriteCond %{QUERY_STRING} ^m=([a-zA-Z0-9]+)$
RewriteRule ^index.php$ user%1.php [L]
How to make it?
Hope this simple configuration will help you out. You can check this configuration here
This will redirect request of this pattern from http://localhost/admin-s/index.php?m=user to http://localhost/admin-s/user
Explanation:
1. If REQUEST_URI is /admin-s/index.php
2. If QUERY_STRING is m=someWords
3. Redirect to /admin-s/someWords
.htaccess
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_URI} \/admin\-s\/index\.php
RewriteCond %{QUERY_STRING} m=([\w]+)
RewriteRule ^(.*)$ /admin-s/%1? [R=301,L]
The question was answered here : How to turn dynamic URL into static URL
For your problem may be try following rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^admin-s/([^-]+) /index.php?m=$1 [R=301,QSA,L]
Let us know if it works
You can use the following rule in your root/.htaccess or /admin-s/.htaccess :
RewriteEngine on
#redirect /admin-s/index.php?m=user to /admin-s/user
RewriteCond %{THE_REQUEST} /admin-s/index\.php\?m=([^\s&]+) [NC]
RewriteRule ^ /admin-s/%1? [L,R]
#rewrite the new uri back to the orignal location
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:admin-s/)?(.+)$ /admin-s/index.php?m=$1 [NC,L]
Try below in your admin-s direcotry,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ index.php?m=$1 [L]

.htaccess How to match only the first appearance of index.php in the URL?

I have the following question. Let's say I have link like
http://localhost/test/index.php/admin
I have the following htaccess:
Options -MultiViews
Options +FollowSymLinks
Options -Indexes
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) $1$2 [R=301,NE,L]
What it does is simply remove index.php from the URL. However, when I enter url like this:
http://localhost/test/abc/blabla/index.php
It goes on an infinite loop. What I want is to do nothing when another index.php is met in the URL. How can I achieve this? Thanks!
Replace your code with this:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /test/
RewriteCond %{QUERY_STRING} !(^|&)r(&|$)
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ $1$2?r [R=302,NE,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!index\.php/).+)$ index.php/$1 [L,NC]

Small error when using .htaccess for clean urls

I am using .htaccess to manage clean URL's but for some reason I get a 404 Not Found Apache error in some cases.
If a user goes to http://domain.com/profile/ everything is fine.
If a user goes to http://domain.com/profile they will get the error
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&cmd=$2 [L]
Try this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /profile/cmd
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&cmd=$2 [L]
# /profile
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
</IfModule>
You can redirect the url with the url which has trailing slash using 301 redirect
Try the following along with your rewrite condition. This will redirect any url request to url with trailing slash. ex. http://domain.com/profile will be redirected to http://domain.com/profile/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
The rewrite rule you wrote requires one slash '/'.
Try this instead:
RewriteRule ^([^/]*)(/([^/]*))?$ /index.php?page=$1&cmd=$3 [L]

Categories