I have a very peculiar requirement, hopefully I can explain it without being too confusing. I created a page template where I list some state's city, let's say the URL is like this: http://www.example.com/states/?q=ohio
and i would like to do it like http://www.example.com/states/ohio/
i also used add_rewrite_rule but it's does not given me output that i want.
so how could i do fix ?
It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, http://www.example.com/states/ohio/ and then apache chops it up using a rewrite rule making it look like this, http://www.example.com/states/?q=ohio, to the server. You can find more here: Rewrite Guide
Web server side
Since you're running Apache, you can create a RewriteRule to rewrite the URI to something that the application understands but have vanity/pretty URIs to your endusers.
For example;
RewriteEngine On
#Rewrites a request like http://www.example.com/states/ohio/ to http://www.example.com/states/?q=ohio at application level
#Having the /? at the end makes the ending slash optional
RewriteRule ^states/([^\/]+)/?$ /states/?q=$1 [L]
Tested with http://htaccess.mwl.be/
Wordpress side
You can use add_rewrite_rule to rewrite the URLs as you desire
add_rewrite_rule('^states/([^\/]+)/?$', 'states/?q=ohio$matches[1]', 'top');
Otherwise, you'd have to manually modify each href element on an anchor tag (<a />) to point to /states/ohio/ instead of /states/?q=ohio.
Related
I have created the website like WOWcrush http://phone77.ml I want the url be like phone77.ml/Naveen in title it should have my name but there come ? at the last of the URl like http://phone77.ml/?Naveen only if use ? it works fine or it shows 404 error please help
You will need to use the .htaccess file in order to point /Naveen to the coresponding get var.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^/.]*)/?$ index.php?section=$1 [L]
or you can be more specific like this
RewriteRule ^/Naveen$ index.php?section=Naveen [L]
$_GET['section'] will contain Naveen and you can use that in your code to show the correct view
By default everything what goes after the '?' symbol is parameters in url. It can be changed, but for that is necessary to properly configure a web server (e.g. Apache or Nginx).
Also can be necessare to made some changes in code or/and configuration of your application.
You can see the question mark as a 'where'. So what you are saying is;
http://phone77.ml/ 'WHERE' Naveen. It all depends on how you build your site, but apparently you have a clause looking for Naveen in the URL. Maybe something like; if(isset($_GET['Naveen'])){...}.
You could also have separate documents for each you pages, then you would link to to that document instead and the URL would look like http://phone77.ml/Naveen.php f.ex.
You might also be interested in looking up the .htaccess document https://httpd.apache.org/docs/current/howto/htaccess.html where you can manipulate the URL from.
i have link
http://www.raffaello-test.com/static/frontend/deutsch/mode-bilder/mode-herren-krawatten/gianni-versace-herren-krawatten.jpg
but i need to hide "/static/frontend" remove this part from URL and get get image from this URL
http://www.raffaello-test.com/deutsch/mode-bilder/mode-herren-krawatten/gianni-versace-herren-krawatten.jpg
i think it will be from .htaccess
One of the option is to create a symlink in the document root.
Second option is to use htaccess rewrite rule. Something like the following should work fine:
RewriteRule ^((deutsch|english)/.+/.+/.+\.(jpe?g|png|gif))$ /static/frontend/$1 [L]
If you have more languages just add them in first parenthesis, like (deutsch|english|russian).
Also note that It's not recommended to use rewriting in high load production for static content, better use symlinks
In the root folder of my project there are several other folders I am calling "apps". The path to one of these apps is something like: /root_folder/myapp/...
There are actually a dozen apps like this. So I want my project to be using friendly URLs, and to achieve that I am using the apache module mod_rewrite.
I added the following rule to my .htaccess (that is in root_folder, just so you to know) RewriteRule ([^/]*)/(.*)?$ myDomain.com/$1/index.php?params=$2 [NC,L]
So an URL such as mydomain/myApp/param/value/param2/value would be translated to mydomain/myApp/index.php?params=value/param2/value
I performed some tests and saw it working until I added the $1 to refer to the app folder (have a look: the_path_to_my_root_folder_here/$1/index.php?params=$2)
The path to one of these apps is something like: `/root_folder/myapp/...**
It is generating an URL like: myDomain.com/myApp/index.php?params=index.php
Well I thought it would be a recursion issue. So it seems that Apache will try another redirection after the first is performed, and then it will generate an URL like that
I found this thread in Stack Overflow
The problem with the answer is that it`s assuming I know when to stop.
Do you know how to make this second redirection to stop?
I am trying the following rule now
RewriteRule myDomain.com/([A-Za-z0-9-]+)/(.*)$/ myDomain.com/$1/index.php?params=$2 [NC,L], but it's not working properly. It is only matching the regex, if I do not pass parameters after the app name. so when I try myDomain.com/user/ it works (not receiving parameters), and fails when I try myDomain.com/user/products/1000/, for example. Instead of rewriting/redirecting, it is trying to find a folder products inside user and etc
Try changing your rewrite rule to something that will match the input url but not your output url. Something like this:
RewriteRule ^([^\/]+)\/([a-z]+)\/(.+)$ /$1/index.php?params=$2/$3
Place this rule in DOCUMET_ROOT/.htaccess:
RewriteEngine On
RewriteRule ^([^/]+)/(.+)$ /$1/index.php?params=$2 [L,QSA]
Unable to understand what you mean by the_path_to_my_root_folder_here
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?
this is the URL path I currently use:
/index.php?page=1&title=articles
I want to get the URL path as
/index/page-1/title-articles
using SEO user friendly URLs in PHP.
And how to get the value of the "title"? Any one can help me pls.
Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.
You need to ensure two things:
your application prints out the new URLs properly, and
your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.
The first part can be simply accomplished by using
echo ' … ';
instead of
echo ' … ';
The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:
RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]
The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.
I want to get the URL path as
/index/page-1/title-articles
Why? I’ve got two objections:
index is a no-information and I doubt that it belongs in the URI
page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.
Thus, I propose either:
/‹article›/1
or
/‹article›/page/1
or
/‹article›/page=1
or
/‹article›[1]
or
/articles/‹article›/page/1
Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).