Remove folder from url slug - php

I have little to no experience with the following problem so I am turning to the community for help.
My Forum is installed within a folder namedforum within my root directory. When users try to login from a discussion or a thread, they get redirected to a 404 url.
The reason why they are getting redirected to 404-page is because the forum creates a second forum slug within the url, here is an example:
When Viewing a Thread
http://www.example.com/forum/discussion/1/letsTalk
The above is the correct url, HOWEVER, when user tries to login they get redirected to the following url
When Logging IN
http://www.example.com/forum/forum/discussion/1/letsTalk
Notice the forum appearing twice in the above url.
Solution
I am guessing this can be fixed within the htaccess file by creating a re-write rule, which is simple enough. However my problem comes in when users view other threads with a different url than the one given above.
Thus, I need to find a way to remove the second forum appearing within the URL which...I am not sure how to tackle? Can this be fixed within the htaccess or will source code need to be modified?

In your head section in the html you should put
<base href="http://www.example.com/forum"/>
This will make relative urls get prepended with the given base.
Then you should make another .htaccess inside the forum subdirectory
It should contain the following:
RewriteEngine On
RewriteBase /forum/
This will make relative URLs used in your backend get prepended with the base.

Related

htaccess change requested filename

I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.

Change wordpress post url (remove subdomain)

I have a Wordpress blog installed in a subfolder and I would like to replace a part of the url of the post.
Right now it is like this: http://domain.com/wordpress/{permalink-structure}
What I would like to achieve (without moving the site) is: http://domain.com/{permalink-structure}
I tried changing the site url (and followed the instructions provided by WP on how to do this), but if I do this, my front-end stops working ( I am not retrieving the posts etc through the WP-API and I work with AngularJS)
Is there an easy way to automatically modify the (by wordpress generated) url of all future posts by using a plugin or modifying the source code? Or via .htaccess?
Redirecting via htaccess is my preferred option but when I tried this, the wp-api plugin (accessible via domain.com/wordpress/wp-json/* stopped working
Just to be clear: I am not accessing the posts by the generated URLS, I retrieve them via the API and the only thing I want is that the link object in the retrieved post has a different URL.
I don't know. Just try to help. This is my solution redirect what need to redirect.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^wordpress/wp-json/(.*)$ http://domain.com/wordpress/wp-json/$1 [R=301,NC,L]
RewriteRule ^wordpress/(.*)$ /$1 [R=301,NC,L]
you want to change wordpress domain or URL. you need to perform following steps.
1 you need to modify all url's stored in database. I prefer to use https://interconnectit.com/ tool. It is pretty good and easy.
2 Once you update all database urls then you need to login in wp-admin and just update permalink settings.
you need to check plugins too. Some plugins may get deactivated you need to activate them again.
Hope this will help.
check word press codex:
https://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
you can display your word press without move your directory from sub folder to root folder and can remove sub-directory from URL.

Change what a link looks like with .htaccess

I have a Joomla based community site and with search engine friendly URLs activated in the backend my profiles are located under mysite.com/community/profile/user/"username"
I need the htaccess file to do nothing unless a URL containing "community/profile/user" is found. If that string is found then it should change the link to mysite.com/"username" but in reality be showing the page mysite.com/community/profile/user/"username"
I think this would be rewrite rule instead of redirect, but I barely know what I'm talking about.
Can someone please tell me what code I must place in my .htaccess file in order to change this? I believe .htacces would be the best way to do what I need, but if you have another idea I'm glad to hear it.
First be sure you understand .htaccess's role.
It is only read when an incomming request is made. So it will not change URLs generated by joomla.
You can however allow urls like mysite.com/eddie to actually pull content from mysite.com/blah/blah/eddie
http://httpd.apache.org/docs/current/rewrite/remapping.html
If you are looking to "train" your users, you can add a step before that to redirect the URL as well. This get's very tricky though as if you're not careful you can get caught in a loop.
user clicks mysite.com/blah/blah/eddie
apache redirects to mysite.com/eddie
(browsers makes second request, user sees URL change)
apache sees mysite.com/eddie and loads the underlying mysite.com/blah/blah/eddie
An easier solution might be to tweak the joomla community code to generate the short urls (mysite/eddie) and use apache to make a call direct to the plugin (mysite/components/communit/index.php?user=eddie

PHP, MySQL: Security concern; Page loads in a weird way

I am testing the security of my website. I am using the following URL to load a PHP page in my website, on localhost:
http://localhost/domain/user/index.php/apple.php
When I do this, the page is not loading normally; Instead the images, icons used in the page simply vanish/disappear from the page. Only text appears. And also on any link I click on this page, it brings me to this same page again without navigating to the required page. So if I have hyperlinks to other pages, such as "SEARCH", which points to search.php, instead of navigating to the search.php page, it refreshes the index.php page and just appends the page name of the destination page to the end of the URL.
For example, say I used the link above. It then loads the index.php page minus the images at it's will. When I click on the "Search" link to navigate to the search page, I see the following in the URL:
http://localhost/domain/user/index.php/search.php
I have a redirection configured to a 404 error page in my .htaccess file, but the page does not redirect to the 404 error page. Notice the search.php towards the end of the URL above. Any other link that I click, reloads the index.php page and just appends the destination page name to the end of the URL like I have shown above.
I was expecting to see a 404 Error but that does not happen. The URL should not even be able to load the page because I do NOT have a "index.php" folder in my website.
What can I do to solve this? All help is appreciated.
Update:
The security concern is that users being able to see a non-existing page (which is quite misleading) like http://localhost/domain/user/index.php/apple.php especially when it does not exists. This makes me feel that this is going to open doors for hackers to exploit the website and compromise its integrity. Can this happen in such a case? I want users to see a 404 error page and any I am willing to any change needed in .htaccess file to accommodate this.
Can you suggest me of some code that I can add to my .htaccess file to accomplish this?
Thank you.
EDIT1:
Here are the contents of my .htaccess files. I have 2 of them. One in domain root and the other in 'user' folder/directory.
/*.htaccess in domain root*/
ErrorDocument 404 /domain/404.php
/*.htaccess in user folder*/
ErrorDocument 404 /domain/user/404.php
EDIT2:
#Pekka Thanks for the link. I added the following code in the .htaccess file (within the user directory)
<Files "mypaths.php">
Options +Includes
SetOutputFilter INCLUDES
AcceptPathInfo Off
</Files>
But still this does not show me the 404 page. Sorry, I am very novice with the .htaccess. Hope you will be able to tell me what I am wrong. Thanks.
The behaviour of why this loads a page:
http://localhost/domain/user/index.php/apple.php
is easily explained. The request is passed to index.php, with apple.php being in the $_SERVER["PATH_INFO"] variable.
So you are in the /user directory as far as the server and the PHP script are concerned.
This is also why no 404 turns up: index.php is always found, no matter which file you specify as the last file.
The browser, however, interprets index.php not as a file, but as the parent directory of apple.php.
Therefore, every relative link you put on the page, say to contact.php is fetched like this:
http://localhost/domain/user/index.php/contact.php
which obviously won't work.
What you may want to do is use absolute paths in images and links, but either way, this is of no concern to security whatsoever.
As a side note, this whole phenomenon is sometimes used to create search engine friendly URLs without having to use mod_rewrite module.
You can turn this behaviour off using the AcceptPathInfo directive.
As for the images, you just have to learn to use absolute paths to your images, which is absolutely necessary
Just instead of images/head.jpg write /images/head.jpg and you will have all your images and styles in place.
As for the /user/index.php/apple.php - why do you want such an odd address?
Why not to use just user/apple.php?
And where is security in your question?

How can I get the value in the URL and modify it for paging with mod-rewrite on?

I am trying to get my old paging class to work which relied on the variables available with $_GET in PHP to reconstruct the URL for the paging links, it worked great but now I am changing to have "pretty url's" with mod-rewrite.
So this
domain.com/?p=the-pagename-identifier&userid=12&page=3
would now be
domain.com/the-pagename-identifier/12/page-3
Problem is my PHP for my old paging would rely on the GET variables to re-construct the URL and make sure any variables present in the URL remain in the new URL's it makes for new pages, now I am stuck because I need it to work with the "virtual" directories that it appears I have in the URL, domain.com/mail/inbox/page-12 is really in the root directory running through my index file domain.com/index.php?p=mail.inbox&page=12
I am lost because some pages will have more things then others in the GET part of the URL.
Since all pages are loaded through the index.php at root level I could almost just link the pages without the full URL path but instead of something like domain.com/mail/page-2 it would end up being domain.com/page-2 since the mail directory in the example is not a real directory. So is it possible to get the value in the URL of a page that was made with mod-rewrite so I can make it think it is in a subfolder and just add the page number onto the current URL of a page?
This rewrite rule:
RewriteRule ^/([0-9a-zA-Z-]+)/([0-9]+)/page-([0-9]+)$ /index.php?p=$1&userid=$2&page=$3
Would transform this:
domain.com/the-pagename-identifier/12/page-3
Into this:
domain.com/?p=the-pagename-identifier&userid=12&page=3
Completely transparent to the end user.
So aside from generating new URL's, your application need not change at all.

Categories