Like on the title, I'm looking for a way to change a url with parameters (from this www.animevid.net/player/?anime=d/dmc to this www.animevid.net/player/anime/d/dmc) using the .htaccess file.
I've found many similar post but I've only got errors, loop redirect, or...nothing. The nearest thing i've got is this code :
RewriteRule ^player/([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+) player/index.html?anime=$1&t=$2 [NC,L]
Also please note the "d/dmc" on "?anime=d/dmc", is a variable, another example is "c/codegeass", "s/sao" etc...
As mentioned above the final, rewritten url you give as an example is invalid. You should escape the last /:
https://www.animevid.net/player/anime/d%2Fdmc
and rewrite to:
https://www.animevid.net/player/?anime=d%2Fdmc
Then you have to change your regex strategy for this to work. Try something like that:
RewriteEngine On
RewriteRule ^player/([^/]+)/([^/]+) player/index.html?anime=$1&t=$2 [NC,L]
Related
I'm having e commerce website in php. And now moving to fancy urls, url rewriting.
for that i've variable length of parameters with optional variables too.
for example,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
Here, the parameters are optional That means, some variables may or may not participage in the url except id.
here, page=e is fixed and it is not dynamic. ( the reason behind is i've other rewritten rules like ^categories, ^toys etc..
and re-written url should be one of these,
http://example.com/e/23/color-blue/size-xxl/
http://example.com/e/26/color-black/width-500/height-900/
http://example.com/e/56/type-shirt/color-white/height-345/size-xl/
i've tried below with no luck,
RewriteRule ^e/([^/.]+)/?$ product.php?page=e&url=$1 [L,NC,QSA]
i'm getting all $_GET values like this,
http://example.com/product.php?page=e&id=23&color=blue&size=xxl&width=200&height=100
but i'm trying to achieve like this,
http://example.com/e/23/color-blue/size-xxl/width-200/height-100
how can i pass the queries with slashes and not &'s.
that main idea behind above last url is to process the whole fancy url into simple one in php,
and then use those variables in the page script. is there any better solution than this ??
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(e)/([0-9]+)/([^-]+)-([^/]*)(/.*)?$ product.php$5?page=$1&id=$2&$3=$4 [L,QSA]
RewriteRule ^(product\.php)/([^-]+)-([^/]*)(/.*)?$ $1$4?$2=$3 [L,QSA]
My original URL is customers.php?id=3 and I get customer ID by writing $_GET['id'].
Now I wrote rewrite rules in htdocs and my URL is customers/id/3. I'm trying to get customer ID by $_GET['id'] which is not working. Can anyone please tell me what should I do to get ID?
My URL Rewrite is
RewriteEngine On
RewriteRule ^customers/([^/]*)$ /customers.php?id=$1 [L]
Your rewrite rule is wrong; you are capturing everything after the customers/ section until the first forward slash and you are not allowing anything after that.
In you example the rule would not even trigger as your url has a forward slash and characters after it.
You should change:
RewriteRule ^customers/([^/]*)$ /customers.php?id=$1 [L]
to something like:
RewriteRule ^customers/id/(\d+).*$ /customers.php?id=$1 [L]
^^ Allow for extra stuff after the id so you could add the name of your customer or something like that
^^^^^ Require at least one digit and only digits
You could also get rid of the ID if you wanted to so that the number would come directly after customer/ but that is up to you.
If you use:
customers.php?id=3
Use:
$_GET['id']
not $_GET['customers'].
If you want to get "customers", use: customers.php?customers=3
Your rewrite will be like this:
RewriteRule ^customers/id/([0-9]+)$ customers.php?id=$1 [L]
There is a simple way to parse urls of that style (customer/id/123):
$aUrlPieces = explode('/', $_SERVER['REQUEST_URI']);
print_r($aUrlPieces);
The last 3 elements in the array will be "customer", "id" and "123".
You can go one step further and define action methods in your application that would take corresponding amount of parameters, that way your urls will be syntax checked by PHP.
In this case, action method "customer" would take 2 parameters $identifier_type and $identifier_value.
That way if you accidentally mistype "customers/" instead of "customer/" you will get "Method not defined" exception.
change your rewrite rule to this:
RewriteRule ^id/([^/]*)$ /customers.php?id=$1 [L]
How do I strip a part of the url? I do not know much about htaccess or apache.
I would like to strip www.mysite.com/page=services to www.mysite.com/services for example.
What exactly do I need to put in the .htaccess file in order to achieve this, and would that work for other pages as well?
Thanks.
I tried this for one of my sites lately and came up with this and for me it works fine,
this goes into the .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z]+)/?$ index.php?page=$1 [NC,L]
then if you would write yoursite.com/pagename it would send yoursite.com/index.php?page=pagename to your php.
You will link to the page like so: yoursite.com/pagename
it wont change yoursite.com/index.php?page=pagename to yoursite.com/pagename in the adress bar after you send it.(if that makes sense :) )
I hope this is what you are looking for...
I think you mean your original URL to be www.mysite.com/index.php?page=services and not www.mysite.com/page=services
Also, you probably mean the opposite, you should switch www.mysite.com/services to www.mysite.com/index.php?page=service
Anyway, to change www.mysite.com/services to www.mysite.com/index.php?page=services then you need .htaccess, and the rule for that would be RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
As suggested earlier, you should read about .htaccess, regex, and rewrite rules. Best resource is the apache documentation here: http://httpd.apache.org/docs/2.2/howto/htaccess.html
I read your comment earlier that you need the opposite, I am not sure why you need that, because the whole idea of URL shortining is to make easy-to-remember URLs in addition to some security concerns that can be resolved. The URL is the first thing that is sent to load your webpage, then .htaccess changes it to some form undrstandable by PHP then PHP deals with get parameters for example.
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.