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
Related
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
I understand that .htaccess URL re-write questions have been asked a number of times however, I am really struggling to get these two things working in combination.
My site takes the following url and performs a wildcard search
http://www.localhost:8888/exercises/exercise?q=overehead%20squat&
I am using the following rule to remove all the %20 spaces to give me:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
Gives: http://www.localhost:8888/exercises/exercise?q=Overhead-Squat
The last thing I would like to do is remove the ?q= so the URL looks like this:
http://www.localhost:8888/exercises/exercise/overehead-squat
It is not essential to make it lowercase, however it is desirable.
Many thanks in advance.
Create a directory called "excercise" and place your code file in it (name it index.php), and place this .htaccess
RewriteEngine On
RewriteRule ^(.*) index\.php?q=$1
I have the following situations on my server:
/->
news->
.htaccess
index.php
post.php
...
And the following rules in my .htaccess:
RewriteRule ^(.*)/admin post.php?slug=$1&admin_mode=true [NC,QSA,L]
RewriteRule ^(.*)$ post.php?slug=$1 [NC,QSA,L]
Now I need my URLs to be the following:
If requested www.mydomain.com/news/ -> it should get the index.php
file
If requested www.mydomain.com/friendly-title-of-my-article -> it
should get the post.php file with the query string as indicated in my .htaccess.
Currently I get correctly the post.php with the query string, but when I go to www.mydomain.com/news/ , it's requesting the post.php file.
Please help. Thanks
Use this
#if query string is empty
RewriteCond %{QUERY_STRING} ^$
#match on / for root, or .index.php in request and send to query string version
RewriteRule ^(/|index.php)?$ /index.php?step=1 [R=301,L]
One way to create nice routing is to let everything go to one index.php and control the flow there. It has multiple advantages like being able to query the database and then decide what page to load. That can influence SEO nicely.
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
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.