Is it possible to use htaccess to rewrite by checking multiple directories and not just one?
Here is an example of what I am looking for:
I have 2 files
site.com/profile.php?username1
site.com/project.php?projectname1
I would like to make it where the person only has to type in something like:
site.com/NAME -> the "NAME" can be whatever they choose it to be, either the username1 or projectname1, doesn't matter.
Now the issue that I'm having is can htaccess search through both profile AND project to match the name that the user types in and redirect the user to the proper file?
To clarify....if the person types in site.com/username1.....will it automatically know to go to PROFILE.php?username1?
if the person types in site.com/projectname1.....will it automatically know to go to PROJECT.php?projectname1?
You can't use htaccess to do this, because mod_rewrite doesn't know anything about the process of what makes the NAME a profile or a project, only the two php scripts know anything about that. What you're better off doing is writing some routing code in something like router.php or even index.php, then route everything through it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /router.phpname=$1 [L]
Then in your router.php script, see if the $_GET['name'] is a profile or a project, then route it to either the profile.php or project.php as it sees fit.
Related
I am struggeling with something I am trying to do regarding folder. I have a sub-folder called "admin" so I can access the site as mydomain.com/admin.
However I want that if I say mydomain.com/foobar or mydomain.com/28y282djdhjd, then it should always assume it is "admin".
Of course the rule shoud not apply to existing files and folders but it is specifically to the first folder from the domain root.
It's as if I want the admin to be accessed no matter what I call it (as long as it is not a physical folder or file)...
I am not good with htaccess if someone could guide me I would truly appreciate it.
Thank you in advance.
So, you need to:
Check that the request isn't for a file that already exists;
Check that the request isn't for a directory that already exists;
Only redirect the request, if it refers to the first folder-level from the domain root;
If that's all correct, something like this should do it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)\/?$ http://example.com/admin [L]
I've had a good look through all the other htaccess url rewrite questions, but all of them deal with the reverse of my problem.
The site I am working on takes content from child pages in WordPress and presents them as anchored sections on the parent page. The problem for the site now is that if Google (or the built in search, though that can probably be done in the templates) links to one of the child pages it will direct users to the single page rather than the correct section of the parent page.
I was hoping to come up with a URL rewrite pattern which would reformat the URL to what I need, but it doesn't seem to work... and I know that's because I have done it wrong!
The URL which needs to be rewritten is something like:
website.com/parent-page/child-page/
I need this to be rewritten to:
website.com/parent-page/?subpage=child-page
My initial stab at it looks like this, but I know I've misunderstood something about how to format the pattern.
RewriteRule ^/$1?subpage=$2 ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$
Can anyone help format this correctly or point out where I'm going wrong?
You can use:
RewriteEngine on
# If the request is not for a valid file/directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/?subpage=$2 [QSA,L]
No initial / in htaccess RewriteRule first uri
And optional final /
RewriteRule ^([^/]+)/([^/]+)/$ /$1/?subpage=$2 [L]
The htaccess rewrites didn't seem to work, so I've created a PHP redirect instead by taking the page slug, page parent permalink and mashing the two together.
Not the most elegant solution, I expect, but it works.
Here is what i am trying to achieve
www.example.com/category
www.example.com/category/subcategory
www.example.com/category/subcategory/product
www.example.com/static-page (like /about-us, /contact, /our-services)
(category, subcategory, products, static-pages etc are dynamic text and there is a permalink foreach thing in the database)
if you see all above requests, you will notice that they are just like extending the directory sturcutre one step each time when the link is clicked, e.g, first was category, and then when I clicked on the subcatogery, I was sent to category/subcategory/ and the finally to product page
Anybody can help me how to acheive all this, i have tried lot to achieve this but in vain yet.
Currently i am using this .htaccess
RewriteRule c/(.*)/(.*)/ cat-details.php?permalink=$1&subcat=$2
RewriteRule c/(.*)/(.*) cat-details.php?permalink=$1&subcat=$2
RewriteRule news/(.*)/(.*)/ news-detail.php?news-id=$1&permalink=$2
RewriteRule news/(.*)/(.*) news-detail.php?news-id=$1&permalink=$2
RewriteRule d/(.*)/(.*)/(.*)/(.*)/ download-detail.php?download-id=$1category=$2&subcategory=$3&permalink=$4
RewriteRule d/(.*)/(.*)/(.*)/(.*) download-detail.php?download-id=$1category=$2&subcategory=$3&permalink=$4
As you can see i have to add c/, news/, d/ etc for each link, i am trying to get rid of this and want to make links pretty whitout proceeding c/, news/, d/ etc
About the only way you're going to be able to do this in Apache proper, is to provide info on which URLs map to which scripts. Apache lets you do this via the RewriteMap directive, but that won't work in .htaccess files. Without it, and without the news/ or c/, Apache doesn't have enough info to route the URLs properly.
What you could do, is simply rewrite every URL that doesn't refer to an existing file, to a script that knows which scripts to run in response. A "router" or "front controller" script, they call it. Something like this:
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule (.*) router.php?path=$1 [QSA]
And in the script, you examine $_GET['path'], decide which script should handle the request, and load/include/require it.
I have found a way myself, just posting it if it could help anyone else in trouble just like me :p
RewriteRule ^([^/]*)\/$ /romuniverse/cat.php?permalink=$1 [L]
RewriteRule ^([^/]*)/([^/]*)\/$ /romuniverse/sub-cat.php?permalink=$1&subcat=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.rar$ /romuniverse/download-detail.php?download-id=$1&category=$2&subcategory=$3&permalink=$4 [L]
I'm still a bit fuzzy on the working of .htaccess, and I've looked around but I can't find anything to help this specific issue.
EDIT: I realize there are other questions that seem like they cover this issue, but I checked some and they didn't seem to offer any help I could understand, and I didn't want to hijack them with my own issues.
This is what I have:
Options +FollowSymLinks
#RewriteBase /
RewriteEngine on
RewriteRule /mp3/(.*) http://old.domain.com/mp3/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
As you can see from the last line, the string typed after the server name is actually a URL parameter and depending on that parameter, different content is pulled from the database and that page is displayed on the site.
The problem I'm having is that the client has a content page called "podcast", so they would go to site.com/podcast which should quietly redirect to site.com/index.php?=podcast and load the content for that page. Unfortunately, the client also has a real site.com/podcast/ folder on their server. Because of this, the rewrite is ignored and the browser attempts to load that folder. It either shows a file listing or a forbidden error if I disable the listing.
After some research (I'm still new to htaccess), I learned that the two lines prior disable the rewrite if the path points to an actual file or folder. Unfortunately, commenting out the one with !-d doesn't seem to have any effect, and commenting out both gives me a server error.
Admittedly, part of the problem here was lack of foresight. URL rewrites should have been planned before everything else was put together, but it wasn't until the site was basically completed that I was notified that the client wants "Friendly URLs" that don't include the ?p= part. Regardless, perhaps there is a way to fix this.
Is there some .htaccess trickery I can use that will force the rewrite even if the URL entered points to a folder (not a specific file) that actually exists? As stated before, removing the !-d doesn't seem to help, although I'm not sure why. Perhaps I misunderstand its purpose.
Thank you for any help, and please be lenient with me if I overlooked something obvious. This is an issue presenting itself on the client's live site right now so I feel a little rushed in solving it. Thanks again.
OH YEAH, and the solution can't be specific to /podcast. The way the client's site is set up, when they want to create a new subpage for the site, a new name is saved for that content based on their title for the page and it is possible (unlikely, but still possible) that another page can be created with a name that matches an existing folder on the server.
Here is a note from mod_rewrite documentation:
By default, mod_rewrite will ignore URLs that map to a directory on
disk but lack a trailing slash, in the expectation that the mod_dir
module will issue the client with a redirect to the canonical URL with
a trailing slash.
This explains why mod_rewrite ignores the URL /podcast. I would suggest that you rename physical directories so that do do not (accidentally) match article names.
Another option would be to disable the DirectorySlash setting. This will prevent Apache from redirecting /podcast to /podcast/.
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
Be warned that disabling this setting has side effects. Read through the documentation first.
Change the following line of code:
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
to
RewriteRule ^(podcast([^?]*)) index.php?p=$1 [L,NC]
I am using modx, which is a cms system. It has a friendly url-option and uses a .htaccess like this.
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
If makes the url look like: mysite.com/about and mysite.com/about/contact
I'd like to expand this .htaccess, so I can write myste.com/about/2 without the page 2 actually existing. I'd like to have the ability to access this in a get-variable. Can this be done? I have no knowledge at this area whatsoever.
You could do this in your .htaccess, but a better way might be to create a plugin that fires on the OnPageNotFound event.
myste.com/about/2 does not exist so it will trigger your plugin which will run before sending the user to the 404 page. In this case, you can can split the /2 from the end of the url and use it to pull in your data or send the user to a different page instead of going to 404.
Does that make sense? It's a bit difficult to contribute more without knowing exactly what you want to do :)