htaccess url rewrite url containing question mark - php

I'm having trouble redirecting some urls as below, there are many urls with different cat breeds. Can anyone help with this issue.
Example url to redirect :
http://www.exampledomain.co.uk/cats/database.nsf/catsforsale!openform?Breed=Persian
I want it directing to the url below. My php script should then do some more complex redirect to make the url tidy :
http://www.exampledomain.co.uk/display_pets.php?pettype=Cats&petbreed=Persian
I've tried the rewrite below but it doesnt work, it just doesnt redirect at all, i think it may have something to do with the ? :
RewriteRule ^/cats/database.nsf/catsforsale!openform?Breed=(.*)$ display_pets.php?pettype=Cats&petbreed=$1 [L]

RewriteRule normally does not look at query strings.
You need to use the QSA flag with Rewrite. It combines the query strings and appends it to the target url.
You can try the following:
RewriteRule ^(.*)$ display_pets.php?url=$1 [L]
This will pass the entire source url to the url parameter of the target url. It will also append the query strings of the source url to the target url.
Another option is to match the query string inside RewriteCond.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^/cats/database.nsf/catsforsale!openform$ display_pets.php?pettype=Cats&petbreed=%1 [L]
See following links:
RewriteRule Flags and
Manipulating the Query String

Related

URL redirect in htaccess file

I want to ask how to redirect in htaccess file from same url with query string parameters to same url without query string parameters. For example if my url is www.example.com/abc.php?cid=5&scid=10, I want to redirect it to www.example.com/abc.php
Please note that the url is same, just query string parameters have been removed in redirection. I have written htaccess file code for urls with single query string but with more than one parameters it is not redirecting.
Here is my code for single parameter which is working as expected:
RewriteCond %{QUERY_STRING} cid=7$
RewriteRule (.*) /contact-us.php? [R=301,L]
It redirects contact-us.php?cid=7 page to contact-us.php
But when I'm exteding the code for multiple parameters it doesn't redirect. Here is what I have written in this case:
RewriteCond %{QUERY_STRING} cid=9$
RewriteCond %{QUERY_STRING} scid=14$
RewriteRule (.*) /stakeholders.php? [R=301,L]
This one is supposed to redirect stakeholders.php?cid=9&scid=14 page to stakeholders.php page. Can someone tell me what is wrong in second code?
I have also googled it but almost all the results are about redirecting from one url to another url not to same url with query string parameters removed. Thanks.
You need to connect your query into one condition, like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} cid=9&scid=14 [NC]
RewriteRule ^(.*)$ http://www.example.com/stakeholders.php? [R=301,NC,L]
This will work for the specific query of cid=9&scid=14, if you wanted to do it for any query then you could use cid=(.+)&scid=(.+) instead.
The ? on the end of the rewritten URL stops the original query from appearing on the end of it and it uses a 301 redirection to perform the change.

PHP cant get value from url, im using htaccess

.htaccess
RewriteRule ^classroom$ dashboard.php?section=classroom [NC]
url
http://localhost/elearning/classroom?keyword=all
php
echo $_GET['keyword'];
and the problem is why i cant get the value from the url?
can you guys help me to solve this one? Thanks .
When rewriting this URL, the original URL's query string will not be sent to the new URL.
Place your .htaccess inside the elearning directory.
Change
RewriteRule ^classroom$ dashboard.php?section=classroom [NC]
to
RewriteRule ^classroom/(\w+)/$ dashboard.php?section=classroom&keyword=$1
Access URL:
http://localhost/elearning/classroom/all/
[UPDATE]
The above idea would work, but there's a standard way of achieving this by adding the QSA (Query String Append) flag for your original rewrite rule.
RewriteRule ^classroom$ dashboard.php?section=classroom [NC,QSA]
that will satisfy your request URL
http://localhost/elearning/classroom?keyword=all

Grabbing entire url as variable with htaccess

This is an image rotator that I'm trying to be able to manage completely through the url to avoid having to setup a user login system. With this system, the urls of the images to be added and removed from the list are passed through the url as a variable. When I type out the entire url: http://www.url.com/index.php?add=http://www.url2.com/images/image.png it works fine. When I try to do it with an htaccess file, http://www.url.com/add=http://www.url2.com/images/image.png, I get a 403.
Applicable .htaccess line:
RewriteRule ^add\=(.*)/? index.php?add=$1 [QSA]
This htaccess line works as long as I'm not trying to submit an entire url. Any ideas? If this isn't enough information just let me know.
-- Edit --
Added more information about the url that I'm trying to retreive
Try this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{THE_REQUEST} /add=(\S+) [NC]
RewriteRule ^ /index.php?add=%1 [QSA,L]
Comment from Ultimater and the solution:
The reason is because it's not a valid URL to begin with. You can't have two colons in the path. You can have them in the query string though but not in the path. http://localhost/add=http://localhost/ is an invalid URL. You can, however put a ? in front so it accepts the colon. Or simply omit the scheme. – Ultimater

SEO friendly URL not working using .htaccess

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below
RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1
The actaul URL looks like this
http://localhost/n/article.php?title=this-is-the-first-news-article
but I want it to look like this :
http://localohst/n/article/this-is-the-first-news-article
When I applied the RewiteRule above it does not change to the desired URL
This should do it. You are missing the n. Not sure why you need the word title though.
RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301]. Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]
If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:
RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
it will be RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

htaccess need help rewriting url

I've been trying to rewrite my URL's with a htaccess file.
My project is using my index.php as it's basic controller.
I fetch the pages from the database with the get-value "naam".
Example:
localhost/YourDesigns/YDCMS/index.php?naam=home (this shows the homepage)
localhost/YourDesigns/YDCMS/index.php?naam=about (this shows the about page)
Now i want to rewrite the URLS to be shown like this:
localhost/YourDesigns/YDCMS/home (this shows the homepage)
localhost/YourDesigns/YDCMS/about (this shows the about page)
I have done some htaccess stuff myself and i've successfully removed the "index.php" from the url, but the "naam" still remains.
How do i remove this?
This is my current htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]
Thanks in advance :)
I think your way to see the process it's not totally right, if you want to show the url like you say localhost/YourDesigns/YDCMS/home you have to first replace the url inside your html content to that format, then by using a RewriteRule you call the correct path internally:
RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
this way when an user click on a link the Apache server will use the above regex to convert the url by a rule that basically says : everything after the base url is aparameter value to be passed on the index.php.
Naturally the regex can be modified to suit your needs, the one i've written above it's a basic string pattern.

Categories