mod_rewrite removing name of query string - php

I have big problem with my urls. I know how to remove index.php form url, but problem is that I don't know to remove that "?act=" from url.
My link now is http: //localhost/doctrine/public/?act=test and I want to create it like http: //localhost/doctrine/public/test.
My current .htaccess file placed in public folder:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I need help how to do that.

Use this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /doctrine/
RewriteRule ^(public)/([^/]+)/?$ $1/?act=$2 [L,QSA,NC]
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Before your expression engine rule, you can add:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /doctrine/public/?act=$1 [L]
To internally rewrite a request like http: //localhost/doctrine/public/test to /doctrine/public/?act=test. You need to make sure the links in your content look like /doctrine/public/test instead of the one with the query string. If there are links outside of your control (like google indexed pages) you can add this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /doctrine/public/?\?act=([^&\ ]+)
Rewrite Rule ^ /doctrine/public/%1? [L,R=301]
to point browsers to the URL without the query string.
This unfortunately isn't going to be compatible with your expression engine URLs.

Related

How can I remove a single get parameter and change it to just a slask in PHP?

So my url is like this: http://localhost/gender/?name=robert
What I wanted to do is to remove the name value pair and make it just like http://localhost/gender/robert
I googled and got this one https://stackoverflow.com/questions/21523311/removing-get-parameter-in-htaccess but it seems to complicated for me.
Any help would be much appreciated.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?name=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^robert$ gender/?name=robert&%{QUERY_STRING} [NC,L]
If you want this to work for all pages i.e. /any-page gets served as index.php?page=any-page then use
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d # not a dir
RewriteCond %{REQUEST_FILENAME} !-f # not a file
RewriteRule ^(.*)$ gender/?name=$1&%{QUERY_STRING} [NC,L]
How do these rules work?
A RewriteRule has the following syntax
RewriteRule [Pattern] [Substitution] [Flags]
The Pattern can use a regular expression and is matched against the part of the URL after the hostname and port (with the .htaccess placed in the root dir), but before any query string.

htaccess - insert script name into all URL requests

I have an application which requires an htaccess file that contains the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
But I am getting 404 errors matching the following patterns:
http://107.170.120.88/tileserver/extra5/11/331/794.png
and:
http://107.170.120.88/tileserver/extra5/11/331/796.grid.json
It seems that any URL with the path:
tileserver/extra5/
needs to be instead:
tileserver/tileserver.php/extra5/
but I haven't had much luck writing an htaccess file to fix this.
If I take out the
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
the base script doesn't work, but then if I add something like:
RewriteRule ^tileserver/extra5/(.*)$ tileserver/tileserver.php/extra5/$1
it has no effect. How do I write this rule?
--- UPDATE ---
Here's the full htaccess:
DirectoryIndex tileserver.php
RewriteEngine on
RewriteBase /tileserver/
<FilesMatch "\.mbtiles$">
Order Allow,Deny
Deny from all
</FilesMatch>
RewriteRule ^(.+).jpeg$ $1.jpg [L]
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]
RewriteRule ^tileserver.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
Note that it is sitting in a subdirectory, in which the parent (root) has this htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Which removes index.php from all URLs (for codeigniter). Maybe that's conflicting?
With the datastructure like this:
example.com/tileserver/tileserver.php
and the url for the matching pattern
107.170.120.88/tileserver/extra5/11/331/794.png
which should result in 107.170.120.88/tileserver/tileserver.php?/extra5/11/331/794.png
your .htaccess should look something like this:
#turn on the rewrite engine
RewriteEngine On
#use the as base for your rewrite conditions the following folder
RewriteBase /tileserver
#since we don't want to rewrite the requests pointing directly to this script, leave it
RewriteRule ^tileserver.php - [L]
#if its not a file or a folder rewrite everything to tileserver.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#everything that comes after /tileserver is passed as argument to the php script
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
I suspect the problem might be the [L] flag.
Instead, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [QSA]
RewriteRule ^tileserver/extra5/(.*) tileserver/tileserver.php/extra5/$1 [L,QSA]

htaccess - Need help writing the correct rule

I'm using SimpleMVCFramework
All my routes are working fine, based on the default htaccess file:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
I have a route that expects an id:
http://localhost/mpl/servicos/smvcf/detalhe/37343
But I need the URL the user sees to be friendly as:
http://localhost/mpl/servicos/smvcf/mercedes_benz-a-a_220_cdi_auto-37343.html
I thought something like this would work, but I get a 404:
RewriteRule ([^/]+)-([^/]+)-([^/]+)-([^/]+).html detalhe/$4
Please help.
The user sees the pretty url, but you are not interested in what the seo-title is. What you are interested in is the number that is hidden in it. As the number is in the very back of the pretty url, let's just match on that:
RewriteRule ^[^/]+-([0-9]+)\.html detalhe/$1 [L]
If you are trying to use another rewrite that will not be routed through the default MVC route index.php then you need to make sure you place your new rewrite before those rules. Also that folder you are redirecting to will need to have a index PHP file to do something as well.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)-([0-9]+)\.html detalhe/$2 [L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]

mod_rewrite - add middle tier directory

Pages are currently available at the following addresses:
http://www.domain.co.uk/solutions/article/page-name1
http://www.domain.co.uk/solutions/article/page-name2
http://www.domain.co.uk/solutions/article/page-name3
etc
..but I would like them to be accessible via the following addresses in order for enhanced SEO:
http://www.domain.co.uk/solutions/page-name1
http://www.domain.co.uk/solutions/page-name2
http://www.domain.co.uk/solutions/page-name3
So, I think I want to use mod_rewrite in the .htaccess file of the site root to add the 'article' directory after the solutions directory on each incoming page request. I have been looking for a suitable answer for about a month and I've tried learning the mod_rewrite basics tutorials but I just can't make this work for me so apologies for another mod_rewrite question.
This is my .htaccess file at present:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
I'm doing something similar on a site of mine. Here's the .htaccess rules I'm using to remove index.php and show the shortened url:
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(css|js|gif|jpe?g|png|ico) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
RewriteCond %{REQUEST_URI} ^/solutions/(.*) [NC]
RewriteRule ^(.*) /solutions/article/%1 [L]
Remember that the URL you see in your browser's address bar is not the URL that ExpressionEngine sees - EE sees {segment_2} as "article".
Edit :
Try this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^solutions/article/
RewriteRule ^solutions/(.*)$ /solutions/article/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Categories