How to remove middle part from URL - mod_rewrite - .htaccess

How to rewrite this url "www.domain.com/index.php?route=custom/static/page" to "www.domain.com/page" in htaccess file, basically just want to take out index.php?route=custom/static/ from urls.
I don't know regex so I tried http://www.generateit.net/mod-rewrite/, but it only generates
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
which doesnt remove 'custom/static' from URLs, I tried a few other examples as well but only removes index.php? and doesnt pass variable, any help is appreciated.

Do you know the concept of using mod-rewrite?
In your question you have mentioned to use mod-rewrite to redirect
"www.domain.com/index.php?route=custom/static/page",
Here $_Get['route']="custom/static/page"] $url_parameter=$_Get['route']
to
"www.domain.com/page" [here $_Get['route']="page"],
So now you can mannually add "custom/static/" to the obtained value of $_Get['route']. as $url_parameter="custom/static"+$_Get['route'] //For PHP
Using your mod_rewrite you can fulfill your demands,
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?route=$1 [L]
But if you need out of box solution using .htaccess then I suggest learning "rewrite-engine" instead of using generating tool

Related

rewrite rule to convert php querystring to html

Please help me in writing correct .htaccess code to get the wanted result.
I have the following pages:
www.mydomain.com/?exam=/hp-certification/
www.mydomain.com/?exam=/cisco-certification/
www.mydomain.com/?exam=/oracle-exam-prep/
www.mydomain.com/?exam=/faq/
www.mydomain.com/?exam=/support/
And there are many more pages but you can notice that the fixed part is: www.mydomain.com/?exam=
Instead of using query string links, I want the following links to work as above pages:
www.mydomain.com/hp-certification/
www.mydomain.com/cisco-certification/
www.mydomain.com/oracle-exam-prep/
www.mydomain.com/faq/
www.mydomain.com/support/
and so on.
your link should be like this www.mydomain.com/?exam=hp-certification/ and the rewrite rule would like this in .htaccess file.
RewriteEngine On
RewriteRule ^([^/.]+)$ /?exam=$1 [L]
You can do that by using the following rules in your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} exam=(.+) [NC]
RewriteRule ^(.*)$ http://www.example.com/%1? [R=301,NC,L]
So what does the above do?
First, it will take the query exam= as a condition, if this condition is met then it will grab any version of the variable using (.+).
It will then rewrite the URL using 301 redirection to show http://www.example.com/anything/. The use of %1 is to grab the variable from exam= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.

htaccess rewrite URL from subdirectory

I've been trying to set up a rewrite rule in our htaccess. We have a search component that directs to the URL:
http://www.domain.co.uk/component/search?searchword=word&searchphrase=all&start=10
We'd like to show it as:
http://www.domain.co.uk/search/word/10
I've been trying this with no avail so far, this is what I have.
RewriteEngine On
RewriteRule ^search/([^/]*)/([^/]*)$ /component/search/?searchword=$1&searchphrase=all&start=$2 [L]
Is there something missing? Could other rules be interfering with it?
At least you should escape the slashes and the question mark
RewriteRule ^search/([^/]*)/([^/]*)$ /component\/search\/\?searchword=$1\&searchphrase=all\&start=$2$ [L]
And you should not put ampersands (&) in your query string - use %26 or & instead...
You generate your link in the first place as
http://www.domain.co.uk/search/word/10
and use .htaccess such as
RewriteEngine On
RewriteRule ^search/([^/]+)/([^/]+)/? /component/search/search.php?searchword=$1&searchphrase=all&start=$2 [L]
to rewrite it to the dynamic form your search.php script can handle. Your attempt basically looks OK (except that you need search.php, not search), so you might want to check if "mod rewrite" is enabled on your server. You might have to wrap the above code in
<IfModule "mod_rewrite.c">
RewriteEngine...
</IfModule>
Consult with your hosting service.

.htaccess Rewrite rule doesn't work

I know there are already a ton of these questions but I don't get it...
I'm trying to rewrite my /register.php to /register but I can't.
.htaccess code
RewriteEngine on
RewriteBase /
RewriteRule ^register$ register.php [L]
Does any of you know the answer?
Sorry for the stupid question but I'm kinda new to the whole .htaccess stuff...
Do you have enabled the Apache module mod_rewrite?
I use this pattern to create user-friendly URLs. It's pretty variable. It means, that every address in format letters-numbers with a certain file extension (PHP or HTML) will be translate into letters-numbers.php (for example).
RewriteRule ^([a-z]+)-([0-9]+)\.(php|html)$ /$1-$2.$3 [L]

how do i setup in htaccess products/item-name

my url currently looks like this
domain.com/products?product=item
and as for products i used
RewriteRule ^products$ products.php [L]
but i want to setup the url the first way do i keep the above rewrite or remove it if i want to use
the first method?
this is what i tried but it wont work
RewriteRule ^products/([a-zA-Z])$ products.php?product=$1 [L]
Try this:
RewriteEngine On
RewriteCond "%{REQUEST_URI}" "!products\.php"
RewriteRule "^products/([^/]+).*" "products.php?product=$1" [L,QSA,PT]
The [PT] is frequently forgotten.
If you're using Apache 2.2.12 or later, consider using the FallbackResource directive instead. See http://httpd.apache.org/docs/trunk/rewrite/remapping.html#fallback-resource for a more detailed description of the various options available to solve this problem.

rewrite php file in htaccess with our without query string

So I have photographs.php, and there are also sections where it will be rewritten as photographs.php?cat=somecategory
Question is, in .htaccess how do I get these both to work as such
/photographs
and
/photographs/somecategory
so that the $_GET['cat'] variable will be = somecategory
/photographs/(.+) will need to redirect to photographs.php?cat=$1, while /photographs/? will just redirect to photographs.php. if you want to be clever, you can combine it and do it in one line and /photograph will just go to photographs.php?cat=[blank].
First of all you must have the mod_rewrite module activated.
Here's what you need to add to your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^photographs$ photographs.php [NC]
RewriteRule ^photographs/(.*)$ photographs.php?cat=$1 [NC]
Just some simple replace rules. You can find lots of info on the web about them.

Resources